【发布时间】:2019-02-14 13:29:04
【问题描述】:
我有两种我正在尝试解决的问题。
第一件事:
其中之一是使用合并将两个对象组合在一起以消除重复行:
$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;
第二件事:
我正在尝试使用 map 函数来为选择字段创建一组用户列表。它使我成为另一个数组中的一个数组,我不知道如何摆脱它。
$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 )
感谢您的帮助和支持...!
【问题讨论】:
标签: php laravel collections