我不知道你有多严格
Client::findOrFail(1)->digitalManagers()->get()
extending collections
在我看来可以解决你的问题。据此,该判决将改为
Client::findOrFail(1)->get()->withAllManagers()
。您将能够在自己创建的宏中实现代码段的逻辑。
下面的代码片段显示了应该在服务提供商中定义的宏的定义。
use Illuminate\Support\Collection;
Collection::macro('withAllManagers', function () {
foreach ($this as $client) {
$client = $client->load('digitalManagers:id,first_name,last_name,email,avatar', 'managers:id,first_name,last_name,email,avatar');
$definedDigitalManagers = $allManagers = $client->digitalManagers;
if ($client->settings->get('digital_merge_with_global') == true) {
$regularManagersToAdd = $client->managers;
foreach ($regularManagersToAdd as $manager) {
if (!$definedDigitalManagers->contains('id', $manager->id)) {
$allManagers->add($manager);
}
}
}
$client->allManagers = $allManagers;
}
return $this;
});
添加宏后,您将使用它作为
客户端::findOrFail(1)->;获取()->withAllManagers()
.我认为它将解决您的问题,即使它可能不会在查询级别生成。