所以在此应用Drawing
belongsTo
Customer
中。我有数据表
<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
。要加载信息I 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);
}
问题
$customer->title
一起使用?答案 0 :(得分:4)
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
答案 1 :(得分:0)
我不确定你的第一个问题。数据表搜索窗口将搜索所有内容。你想让它专门针对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' },
]
});
});
这是未经测试的,但应该达到你想要的效果。
答案 2 :(得分:0)
您还可以使用与yajra的优雅关系,这是示例代码。
$sub_sectors = Sector::where('parent_id', '>', 0)->with('parent')->latest()->get();
$sub_sectors->each(function($sub_sectors){
$sub_sectors->sector = $sub_sectors->parent->title['en'];
});
在此示例中,您可以通过每种方法获取扇区名称来获得针对子扇区的扇区,现在您可以在yajra表中显示扇区