如何在 Laravel 上编辑列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33675321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to edit column on laravel?
提问by Haroldas
I'm working with Laravel 5.1 + datatables (http://datatables.yajrabox.com/).
我正在使用 Laravel 5.1 + 数据表(http://datatables.yajrabox.com/)。
I need to edit column user_id and show the firstname and lastname from relationships.
我需要编辑列 user_id 并显示关系中的名字和姓氏。
There is my code.
有我的代码。
public function getOrders(Request $request){
if($request->ajax()) {
$orders = Order::with('call', 'contact.company', 'campaign')
->Client()->Finished()->get();
return Datatables::of($orders)->editColumn('user_id',
function ($orders) {
return $orders->User->firstname.' '.$orders->User->lastname;
})->make(true);
}
return view('global/orders');
}
And there is my datatables.js call:
还有我的 datatables.js 调用:
var oTable = $('#orders-data').dataTable({
"processing": true,
"serverSide": true,
"ajax": '/history/orders',
"columns": [
{ data: 'created_at', name: 'created_at' },
{ data: 'user_id', name: 'user_id' },
{ data: 'call.phone', name: 'phone' },
{ data: 'contact.first_name', name: 'first_name' },
{ data: 'contact.last_name', name: 'last_name' },
{ data: 'contact.company.name', name: 'company' },
{ data: 'contact.address', name: 'address' },
{ data: 'contact.postal', name: 'postal' },
{ data: 'contact.city', name: 'city' },
{ data: 'contact.country', name: 'country' },
{ data: 'campaign.name', name: 'name' },
{ data: 'call.call_length', name: 'call_length' },
{ data: 'call.comment', name: 'comment' },
{ data: 'call.status', name: 'status' },
{ data: 'call.full_record', name: 'full_record' },
{ data: 'action', name: 'action', orderable: false, searchable: false }
]
});
But on this $orders->User->firstname
I got 10 queries
但对此$orders->User->firstname
我收到了 10 个查询
select * from `users` where `users`.`id` = 'x' limit 1
How to edit columns with relationships on $orders = Order::with('user', 'call', 'contact.company', 'campaign')->Client()->Finished()->get();
?
如何编辑具有关系的列$orders = Order::with('user', 'call', 'contact.company', 'campaign')->Client()->Finished()->get();
?
采纳答案by Haroldas
Solution was User relationship to be lowercase:
解决方案是用户关系为小写:
return Datatables::of($orders)
->editColumn('user_id', function ($orders) {
return $orders->user->firstname.' '.$orders->user->lastname;
})->make(true);
Here is solution doc: https://laravel.com/docs/5.2/eloquent-relationships#eager-loading
这是解决方案文档:https: //laravel.com/docs/5.2/eloquent-relationships#eager-loading
回答by Flm
Did you tried this ?
你试过这个吗?
return Datatables::of($orders)
->editColumn('user_id', {{ $orders->User->firstname.' '.$orders->User->lastname }})
->make(true);
return Datatables::of($orders)
->editColumn('user_id', {{ $orders->User->firstname.' '.$orders->User->lastname }})
->make(true);
Yours sincerely,
Flemming
您真诚的,
弗莱明