2015-10-13 140 views
0

所以在這個應用程序DrawingbelongsToCustomer。我有數據表laravel數據表關係

  <table id='drawing-table' class="table table-bordered table-hover"> 
       <thead> 
        <tr> 
         <th>Drawing number</th> 
         <th>Customer</th> 
        </tr> 
       </thead> 
      </table> 

這表明$darwing->number$customer->title。加載信息我use yajra\Datatables\Datatables;

數據加載這個JS方法:

$(function() { 
    $('#drawing-table').DataTable({ 
     processing: true, 
     serverSide: true, 
     ajax: '{{route('drawings.datatable')}}', 
     columns: [ 
      { data: 'number', name: 'number' }, 
      { data: 'customer.title', name: 'customer' }, 
     ] 
    }); 
}); 

這Laravel方法:

public function datatable() 
{ 
    $drawings = Drawing::select(array('drawings.id','drawings.number')); 

    return Datatables::of(Drawing::with('customer')->select('*'))->make(true); 
} 

質詢

  1. 如何使數據表中的搜索窗口工作$customer->title
  2. 如何將圖紙編號和客戶標題顯示爲鏈接?

回答

3
public function datatable() 
    { 

     //reference customer table 
     $drawings = DB::table('customers') 
      // join it with drawing table 
      ->join('drawings', 'drawings.customer_id', '=', 'customers.id') 
      //select columns for new virtual table. ID columns must be renamed, because they have the same title 
      ->select(['drawings.id AS drawing_id', 'drawings.number', 'customers.title', 'customers.id AS customer_id']); 

     // feed new virtual table to datatables and let it preform rest of the query (like, limit, skip, order etc.) 
     return Datatables::of($drawings) 
      ->editColumn('title', function($drawings) { 
       return '<a href="'.route('customers.show', $drawings->customer_id).'">' . $drawings->title . '</a>'; 
      }) 
      ->editColumn('number', function($drawings) { 
       return '<a href="'.route('drawings.show', $drawings->drawing_id).'">' . $drawings->number . '</a>'; 
      }) 
      ->make(true); 
    } 

花了很多時間試圖找出它,希望它節省了一些時間。 http://datatables.yajrabox.com/fluent/joins

0

我不確定你的第一個問題。 Datatables搜索窗口將搜索所有內容。你想只針對1列嗎?

要回答您的第二個問題,您可以編輯列輸出。試試這個

$drawings = Drawing::select(array('drawings.id','drawings.number')); 

return Datatables::of(Drawing::with('customer')->select('*')) 
     ->editColumn('customer', function($drawings) { 
      return '<a href="#">' . $drawings->customer . '</a>'; 
     })    
     ->make(true); 

編輯

爲了達到你想要的搜索,你會想要做這樣的事情:

public function datatable(Request $request) 
{ 
    $drawings = Drawing::select(array('drawings.id','drawings.number')); 

    return Datatables::of(Drawing::with('customer')->select('*')) 
      ->filter(function ($query) use ($request) { 
       if ($request->has('name')) { 
        $query->where('customer.customer_name', 'like', "%{$request->get('name')}%"); 
       } 
      }) 
      ->editColumn('customer', function($drawings) { 
       return '<a href="#">' . $drawings->customer->customer_name . '</a>'; 
      })    
      ->make(true); 
} 

然後,在你的JS

$(function() { 
    $('#drawing-table').DataTable({ 
     processing: true, 
     serverSide: true, 
     ajax: { 
      url: '{{route('drawings.datatable')}}', 
      data: function (d) { 
       d.name = $('input[name=name]').val(); 
      } 
     }, 
     columns: [ 
      { data: 'number', name: 'number' }, 
      { data: 'customer.title', name: 'customer' }, 
     ] 
    }); 
}); 

這是未經測試的,但應達到你想要的。

+1

它不會按名稱搜索客戶,因爲繪圖表具有customer_id列,但客戶的標題在客戶表中 –

+0

好的,我更新了我的帖子。如果您有一個與客戶數據庫的關係模型,您可以在其中找到客戶名稱,那麼這將起作用。只要更新了價值觀,我已經猜到了;例如,「customer_name」。 Laravel數據表包中有一個過濾器方法,用於添加您自己的過濾器。 – IllegalPigeon

+0

所以沒有辦法使用常規的搜索字段,因爲這將導致'列未找到:1054未知列'客戶'在'where子句'中?傷心 –