winda

KaryawanController

May 28th, 2017
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.83 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use App\Karyawan;
  7. use Validator;
  8. use Storage;
  9. use File;
  10. use App\Http\Requests\KaryawanRequest;
  11.  
  12. class KaryawanController extends Controller
  13. {
  14.     /**
  15.      * Display a listing of the resource.
  16.      *
  17.      * @return \Illuminate\Http\Response
  18.      */
  19.     public function index()
  20.     {
  21.         //$karyawan_list = Karyawan::all()->sortByDesc('id');//sortir data
  22.         $karyawan_list = Karyawan::orderBy('id','desc')->where('deleted','0')->paginate(3);
  23.         $jumlah_karyawan = $karyawan_list->count();//tampil jml data
  24.         return view('chap8.karyawan', compact('halaman', 'karyawan_list', 'jumlah_karyawan'));
  25.     }
  26.  
  27.     /**
  28.      * Show the form for creating a new resource.
  29.      *
  30.      * @return \Illuminate\Http\Response
  31.      */
  32.     public function create()
  33.     {
  34.             return view('chap8.create', compact('halaman'));
  35.     }
  36.  
  37.     /**
  38.      * Store a newly created resource in storage.
  39.      *
  40.      * @param  \Illuminate\Http\Request  $request
  41.      * @return \Illuminate\Http\Response
  42.      */
  43.  
  44.     private function uploadFoto(KaryawanRequest $request){
  45.         $foto = $request->file('foto');
  46.         $ext = $foto->getClientOriginalExtension();
  47.  
  48.         if($request->file('foto')->isValid()){
  49.             $namaFoto = date('YmdHis')."$ext";
  50.             $upload_path = 'fotoupload';
  51.             $request->file('foto')->move($upload_path,$namaFoto);
  52.             return $namaFoto;
  53.         }
  54.         return false;
  55.     }
  56.  
  57.     public function store(KaryawanRequest $request)
  58.     {
  59.         $input = $request->all();
  60.  
  61.         //upload foto
  62.         if($request->hasFile('foto')){
  63.             $input['foto']=$this->uploadFoto($request);
  64.         }
  65.         //insert data
  66.         $karyawan=Karyawan::create($input);
  67.         return redirect('karyawan');
  68.     }
  69.  
  70.     private function HapusFoto(Karyawan $karyawan){
  71.         $exist=Storage::disk('foto')->exists($karyawan->foto);
  72.  
  73.         if(isset($karyawan->foto)&&$exist){
  74.             $deleted = Storage::disk('foto')->delete($karyawan->foto);
  75.             if($deleted){
  76.                 return true;
  77.             }
  78.             return false;
  79.         }
  80.     }
  81.     /**
  82.      * Display the specified resource.
  83.      *
  84.      * @param  int  $id
  85.      * @return \Illuminate\Http\Response
  86.      */
  87.     public function show($id)
  88.     {
  89.         $karyawan=Karyawan::findOrFail($id);
  90.         return view('chap8.show',compact('halaman','karyawan'));
  91.     }
  92.  
  93.     /**
  94.      * Show the form for editing the specified resource.
  95.      *
  96.      * @param  int  $id
  97.      * @return \Illuminate\Http\Response
  98.      */
  99.     public function edit($id)
  100.     {
  101.         $karyawan= Karyawan::findOrFail($id);
  102.         return view('chap8.edit',compact('karyawan'));
  103.     }
  104.  
  105.     /**
  106.      * Update the specified resource in storage.
  107.      *
  108.      * @param  \Illuminate\Http\Request  $request
  109.      * @param  int  $id
  110.      * @return \Illuminate\Http\Response
  111.      */
  112.     public function update(Karyawan $karyawan, Request $request)
  113.     {
  114.         $input = $request->all();
  115.         $validation = Validator::make($input,[
  116.             'nip' =>'required|string|size:5',
  117.             'nama' =>'required|string|max:35|min:5',
  118.             'tgl_lahir' => 'required|date',
  119.             'gender'=>'required|in:L,P',
  120.             'foto'=>'sometimes|image|max:500|mimes:jpg,jpeg,bpm,png,JPG,JPEG,PNG',]);
  121.  
  122.         if ($validation->passes())
  123.          {
  124.                 //cek ada foto apa ga?
  125.             if($request->hasFile('foto')){
  126.                 //hapus foto lama
  127.                 File::delete('fotoupload/'.$karyawan->foto);
  128.                 $ext = $request['foto']->getClientOriginalExtension();
  129.                 $namaFoto = date('YmdHis').".".$ext;
  130.                 $upload_path = 'fotoupload';
  131.                 $request->file('foto')->move($upload_path,$namaFoto);
  132.                 $input['foto'] = $namaFoto;
  133.                
  134.             }
  135.  
  136.             $update = Karyawan::find($input['id']);
  137.             $update->update($input);
  138.             return redirect ('karyawan');
  139.          }
  140.          return back()->withInput()->withErrors($validation);
  141.  
  142.     }
  143.  
  144.     /**
  145.      * Remove the specified resource from storage.
  146.      *
  147.      * @param  int  $id
  148.      * @return \Illuminate\Http\Response
  149.      */
  150.     public function destroy(Karyawan $karyawan)
  151.     {
  152.         File::delete('fotoupload/'.$karyawan->foto);
  153.         $karyawan->delete();
  154.         return redirect('karyawan');
  155.     }
  156.     public function search(Request $request)
  157.     {
  158.         $cari = $request->get('search');
  159.         $karyawan_list = Karyawan::where('nama','LIKE','%'.$cari.'%')-> orWhere ('nip','LIKE','%'.$cari.'%')->paginate(1);
  160.         $jumlah_karyawan = $karyawan_list->count();
  161.         return view('chap8.karyawan', compact('halaman', 'karyawan_list', 'jumlah_karyawan'));
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment