laravel-7 elequent集合合并不工作
我试图在我的函数中返回一个有说服力的集合,所以我这样做了:laravel-7 elequent集合合并不工作,laravel,eloquent,laravel-7,Laravel,Eloquent,Laravel 7,我试图在我的函数中返回一个有说服力的集合,所以我这样做了: // model // User.php // user -> branch -> groups -> alerts public function alerts() { $alerts = (new Alert)->newCollection(); // empty elequent collection $groups = $this->branch()->
// model
// User.php
// user -> branch -> groups -> alerts
public function alerts()
{
$alerts = (new Alert)->newCollection(); // empty elequent collection
$groups = $this->branch()->first()->groups()->get(); // collection of group model
foreach ($groups as $group) {
// merge all groups alerts to an single eloquent collection
$alerts->merge($group->alerts()->get());
}
return $alerts;
}
然而,我知道我的数据库中有一些数据,我检查了它们<代码>$alerts在我的情况下不能为空
如果我dd($alerts)
只有一组有说服力的集合为空
// dd($alerts)
Illuminate\Database\Eloquent\Collection {#1423 ▼
#items: []
}
另外,我知道push
和concat
方法,但concat
也不起作用,而且push
也不起作用
我知道我可以做连接表,但不想使用连接太多
如何正确合并雄辩的收藏
谢谢
foreach ($groups as $group) {
$alerts = $alerts->merge($group->alerts()->get());
}
您忘记分配合并的集合。很好,它没有注意到分配!愚蠢的错误哈哈。错误发生了;)
// dd($alerts)
Illuminate\Database\Eloquent\Collection {#1423 ▼
#items: []
}