我想从2个表中选择一个数据提供给所有用户,并将此数据合并到要返回的对象中 例 { 用户:{ ID:1, 名称:bla, 保存的卡:[ {id:1,name:test}, {id:2,名称:test2} ] }, {id:2,名称:bla1, 保存的卡:[ {id:1,name:test}, {id:2,名称:test2}, {id:3,名称:test3} ] } }
public function getalluser(Request $request)
{
$User_data = User::where('users.role', '=', 0)
->get();
$count = count($User_data);
for ($i = 0; $i < $count; $i++) {
$json_data[] = [
'user' => User::where('users.role', '=', 0)
->where( 'users.id', $User_data->id[$i])
->leftJoin('webrole', 'users.role', '=', 'webrole.id')
->get(),
'saved cards' => User::where('users.role', '=', 0)
->where( 'credit_cards.user_id', $User_data->id[$i])
->leftJoin('credit_cards', 'users.id', '=', 'credit_cards.user_id')
->get()
];
}
return response()->json($User_data);
}
答案 0 :(得分:0)
foreach($User_data as $data) {
$json_data[] = [
'user' => User::where('users.role', '=', 0)
->where( 'users.id', $data->id)
->leftJoin('webrole', 'users.role', '=', 'webrole.id')
->get(),
'saved cards' => User::where('users.role', '=', 0)
->where( 'credit_cards.user_id', $data->id)
->leftJoin('credit_cards', 'users.id', '=', 'credit_cards.user_id')
->get()
];
}