laravel-7后续集合合并不起作用



我试图在函数中返回一个有说服力的集合,所以我这样做了:

// 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不能为空!

如果I为CCD_ 2,则只有一个CCD_。

// dd($alerts)
IlluminateDatabaseEloquentCollection {#1423 ▼
#items: []
}

我也知道pushconcat方法,但concat也不起作用,push也不能做我想做的事。

我知道我可以联接表,但不想也使用联接。

如何正确地合并有说服力的集合?

谢谢。

foreach ($groups as $group) {
$alerts = $alerts->merge($group->alerts()->get());
}

您忘记分配合并的集合。

最新更新