SHARE
TWEET

laravel datatable manual

kura2yamato Oct 19th, 2019 128 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. public function index_data(Request $request ){
  4.     $draw=$request->input('draw');
  5.     $start=$request->input('start');
  6.             $limit=$request->input('length');
  7.             //dd($user_login);
  8.             $clData = new \App\Modelku;
  9.             $param=[
  10.                 'limit'=>$limit,
  11.                 'start'=>$start,
  12.                 'search'=>$request->input('search')['value'],
  13.                 'order'=>$request->input('order')[0]['column'],
  14.                 'order_dir'=>$request->input('order')[0]['dir'],
  15.                 'raw'=>$user[0]
  16.             ];
  17.     $total_filtered= $clData->dataTableUserid($param,TRUE);
  18.             $data= $clData->dataTableUserid($param);
  19.             unset($param['search'] );
  20.             $total= $clData->dataTableUserid($param,TRUE);
  21.     return view('admin.json.data',compact('draw','user_login','data','total','total_filtered','param','sql_query'));
  22. /*nama view bisa diganti*/
  23. }
  24.  
  25. //==================MODEL================
  26. <?php
  27.  
  28. namespace App;
  29.  
  30. use Illuminate\Database\Eloquent\Model;
  31. use Illuminate\Support\Facades\DB;
  32.  
  33. class Modelku extends Model
  34. {
  35.        
  36.     public function dataTableUserid($params=array(),$count=FALSE ){  
  37.         //default order
  38.          
  39.         $order_by='id';
  40.         $order_dir='asc';
  41.         $ord=isset($params['order'])?$params['order']:-1;
  42.         $ord_dir=isset($params['order_dir'])?$params['order_dir']:-1;
  43.         //custom.. update sendiri
  44.         if($ord==1){
  45.             $order_by='id';
  46.         }
  47.         if($ord==2){
  48.             $order_by='name';
  49.         }
  50.        
  51.         if($ord_dir!=-1){
  52.             $order_dir=$ord_dir;
  53.         }
  54.        
  55.         $data = $this->orderBy($order_by,$order_dir);
  56.        
  57.         //search bila ada pencarian
  58.         $search=isset($params['search'])?$params['search']:'';
  59.         if($search!=''&&strlen($search)>3){
  60.             $data->Where('name','like', '%'.$search.'%')
  61.             ->orWhere('detail','like', '%'.$search.'%') ;
  62.              
  63.         }
  64.        
  65.         //show data or count
  66.         if($count==FALSE){
  67.             $data->skip($params['start'])->take($params['limit']);
  68.         }else{
  69.             return $data->count();
  70.            
  71.         }
  72.        
  73.         $show_query=isset($params['show_query'])?$params['show_query']:FALSE;
  74.         if($show_query){
  75.             return $data->toSql();
  76.         }else{
  77.             return $data->get();
  78.         }
  79.     }
  80.  
  81.    
  82. }
  83.  
  84. //==================VIEW=================
  85. <?php //dd($user_login);
  86. $clData = new \App\Modelku;
  87. $datatable=[
  88. 'data'=>[],
  89. 'draw'=>$draw,
  90. 'recordsFiltered'=>$total_filtered,
  91. 'recordsTotal'=>$total
  92. ];
  93. $pil = $clData->typeKapal();
  94. //dd($datatable);
  95.  
  96. foreach($data as $row){
  97.     $tmp=[
  98.         /*data apa aja yang kamu keluarkan*/
  99.         'id'=>$row->id
  100.          
  101.     ];
  102.    
  103.    
  104.    
  105. }
  106.  
  107. echo json_encode($datatable);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top