我有两种问题要解决。
第一件事:
其中之一是使用合并将两个对象组合在一起,以摆脱线条的重复:
$lead -> foreach of $leads = Lead::all();
$tempUser = DB::table('users')
->select('phone','email', DB::raw('CONCAT(users.first_name, " ", users.last_name) AS name'))
->where('id', $lead->customer_id)
->first();
$lead->name = $tempUser->name;
$lead->phone = $tempUser->phone;
$lead->email = $tempUser->email;
搜索使用合并函数摆脱这3行的方式:
$lead->name = $tempUser->name;
$lead->phone = $tempUser->phone;
$lead->email = $tempUser->email;
第二件事:
我正在尝试使用地图功能来为选择字段制作一系列用户列表。它使我成为另一个数组中的数组,我不知道如何摆脱它。
$mailingLists = MailingList::select('id', 'name')->get();
$lists = collect($mailingLists)->map(function($mailingLists){
return [$mailingLists->id => $mailingLists->name];
})->toArray();
输出:
数组 (( [0] =>数组 (( [1] =>邮件列表 ) [1] =>数组 (( [3] =>邮件列表2 ) )
欲望:
数组 (( [1] =>邮件列表 [3] =>邮件列表2 )
感谢您的帮助&支持...!
您必须使用dride:
$lists = collect($mailingLists)->reduce(function($carry, $mailingLists){
if (!$carry[$mailingLists->id]) {
$carry[$mailingLists->id] = $mailingLists->name;
}
return $carry;
}, []);
对于第一件事,您可以使用fill
或update
方法(填充不会保存信息,您必须调用save
方法mefore)对于数据库调用,使用pluck
方法:
->where('id', $lead->customer_id)
->pluck('name', 'phone', 'email') // getting only these fields
->first();
$lead->update($tempUser); // update direcly with tempUser fields