我的控制器
$ followers = Followers :: where('requested',$ user-> id)-> where('status',1)-> get();
foreach ($followers as $follower){
$followerss = User::with('id', $follower->requester)->get();
}
如果该特定ID有两个或多个关注者,但是im $ followerss变量仅存储最后一个关注者数据,而不存储以前的数据。
预先感谢<3
答案 0 :(得分:1)
那是因为您每次迭代都会覆盖变量$followerss
的值。
您需要先将$followerss
声明为数组,然后再进入循环,然后在循环merge
或push
中将这些项
推送示例:
$followerss = [];
foreach ($followers as $follower){
$users = User::with('id', $follower->requester)->get();
foreach($users as $user) {
$followerss[] = $user;
}
}
合并示例:
$followerss = [];
foreach ($followers as $follower){
$followerss = array_merge($followerss, User::with('id', $follower->requester)->get());
}