2016-04-24 46 views
0

我使用yajra/laravel-datatables laravel包装显示的模型的所有记录列表的变化状态yajra/laravel-的DataTable结果软删除的模型。包括在一个复选框

现在我想改变一个简单的复选框,包括在结果软删除的模型。

这是我的复选框:

<input class="checked-switch" type="checkbox" id="showDeleted" /> 

,这是我的数据表的jQuery代码来发送和检索结果:

$('#allCoursesTable').DataTable({ 
        processing: true, 
        serverSide: true, 
        "bSort": false, 
        "responsive": true, 
        ajax: { 
         url : '{!! route('coursesData') !!}', 
         data: function(d){ 
          d.showDeleted = $('#showDeleted').prop('checked') 
         } 
        }, 
        columns: [ 
         //columns properties come here 
        ] 
       }); 

     $('#showDeleted').change(function (e) { 
      console.log($(this).is(':checked')); 
      allCoursesTable.draw(); 
     }); 

而在后端我写这篇文章:

public function coursesData (Request $request) 
    { 
     $courses = 
      Course::select(['course_id', 'title', 'start_date', 'end_date', 'picture', 'lesson_count', 'status', 'active', 'teacher','start_date','end_date','reg_start_date','reg_end_date']); 


     if ($request->get('showDeleted')) { 
      //what Can I do here??? 
     } 

     $datatable = app('datatables')->of($courses) 
      ->orderBy('created_at', 'desc') 

     // add Or edit columns come here 

     return $datatable->make(true); 
    } 

我知道在纯laravel中包含软删除模型应该这样做:

$Courses= App\Course::withTrashed()->get(); 

但我不知道如何根据复选框状态添加或删除withTrashed()方法。

该怎么办?

回答

0

bobbybouwmann answer at laracasts,解决办法是:

$courses = Course::select(['course_id', 'title', 'start_date', 'end_date', 'picture', 'lesson_count', 'status', 'active', 'teacher','start_date','end_date','reg_start_date','reg_end_date']); 

// It depends on what you pass through, but I would pass 1 or something. 
if ($request->has('checkbox_field') && $request->get('checkbox_field') == 1) { 
    $courses = $courses->withTrashed(); 
} 


$datatable = app('datatables') 
    ->of($courses) 
    ->orderBy('created_at', 'desc') 

,并在JavaScript代码:

data: function(d){ 
    d.showDeleted = $('#showDeleted').is(':checked') ? 1 : 0; 
} 

现在我的东西做工精细。