laravel数据表关系
问题描述:
所以在这个应用程序Drawing
belongsTo
Customer
。我有数据表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);
}
质询
- 如何使数据表中的搜索窗口工作
$customer->title
? - 如何将图纸编号和客户标题显示为链接?
答
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
答
我不确定你的第一个问题。 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' },
]
});
});
这是未经测试的,但应达到你想要的。
它不会按名称搜索客户,因为绘图表具有customer_id列,但客户的标题在客户表中 –
好的,我更新了我的帖子。如果您有一个与客户数据库的关系模型,您可以在其中找到客户名称,那么这将起作用。只要更新了价值观,我已经猜到了;例如,“customer_name”。 Laravel数据表包中有一个过滤器方法,用于添加您自己的过滤器。 – IllegalPigeon
所以没有办法使用常规的搜索字段,因为这将导致'列未找到:1054未知列'客户'在'where子句'中?伤心 –