2016-04-24 47 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; 
} 

現在我的東西做工精細。

相關問題