fadlyshafa

Untitled

May 3rd, 2020
169
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use DB;
  7. use Datatables;
  8.  
  9. use App\Models\M_customer;
  10.  
  11. class Customer_controller extends Controller
  12. {
  13.     public function index(){
  14.         $title = 'List Customer';
  15.         // $data = M_customer::orderBy('nama','asc')->get();
  16.         $yajra = url('customer/yajra');
  17.  
  18.         return view('customer.index',compact('title','yajra'));
  19.     }
  20.  
  21.     public function add(){
  22.         $title = 'Add Customer';
  23.  
  24.         return view('customer.add',compact('title'));
  25.     }
  26.  
  27.     public function store(Request $request){
  28.         try {
  29.             $data = $request->except(['_token','_method']);
  30.             $data['created_at'] = date('Y-m-d H:i:s');
  31.             $data['updated_at'] = date('Y-m-d H:i:s');
  32.  
  33.             M_customer::insert($data);
  34.             \Session::flash('sukses','Data berhasil ditambah');
  35.         } catch (\Exception $e) {
  36.             \Session::flash('gagal',$e->getMessage());
  37.         }
  38.         return redirect()->back();
  39.     }
  40.  
  41.     public function edit($id){
  42.         $title = 'Edit Customer';
  43.         $dt = M_customer::find($id);
  44.  
  45.         return view('customer.edit',compact('title','dt'));
  46.     }
  47.  
  48.     public function update(Request $request,$id){
  49.         try {
  50.             $data = $request->except(['_token','_method']);
  51.             $data['updated_at'] = date('Y-m-d H:i:s');
  52.  
  53.             M_customer::where('id',$id)->update($data);
  54.             \Session::flash('sukses','Data berhasil diupdate');
  55.         } catch (\Exception $e) {
  56.             \Session::flash('gagal',$e->getMessage());
  57.         }
  58.         return redirect()->back();
  59.     }
  60.  
  61.     public function delete($id){
  62.         try {
  63.             M_customer::where('id',$id)->delete();
  64.             \Session::flash('sukses','Data berhasil dihapus');
  65.         } catch (\Exception $e) {
  66.             \Session::flash('gagal',$e->getMessage());
  67.         }
  68.         return redirect()->back();
  69.     }
  70.  
  71.     public function yajra(Request $request){
  72.         $users = M_customer::select(['*'])->get();
  73.  
  74.         return Datatables::of($users)->addColumn('action',function($e){
  75.             $edit = url('customer/'.$e->id);
  76.             // $dt
  77.  
  78.             return '<a href="'.$edit.'" class="btn btn-xs btn-warning"><i class="fa fa-pencil"></i></a>
  79.  
  80.                <button class="btn btn-xs btn-danger btn-hapus" href="'.$edit.'"><i class="fa fa-trash"></i></button>';
  81.         })->make();
  82.     }
  83. }
RAW Paste Data