Laravel 通过深层关系问题隐藏API资源字段/属于多个

Laravel 通过深层关系问题隐藏API资源字段/属于多个,laravel,resources,relationship,Laravel,Resources,Relationship,这里的教程似乎没有达到预期效果 我知道如何仅在必要时使用“whenLoaded”加载关系,但对于不涉及关系的简单字段参数,我无法动态加载这些字段 下面是Agency.php的一个片段 'expected_num_of_b_positive_donors' => $this->getExpectedNumOfDonors("B+"), 'elligible_to_schedule_mbd' => $this-> 'donors' => $this->getDon

这里的教程似乎没有达到预期效果

我知道如何仅在必要时使用“whenLoaded”加载关系,但对于不涉及关系的简单字段参数,我无法动态加载这些字段

下面是Agency.php的一个片段

'expected_num_of_b_positive_donors' => $this->getExpectedNumOfDonors("B+"),
'elligible_to_schedule_mbd' => $this->
'donors' => $this->getDonors(),
 'contact_persons' => $this->getContactPersons(),
'last_mbd' => $this->getLastMBD(),
'average_number_of_donors_per_day' => $this->getAverageNumberofDonorsPerDay()
这一切都会非常容易,如果拉雷维尔通过深厚的关系带着一个属于许多人的关系走出盒子,就像我可以很容易地使用的那样:

'donors' => $this->whenLoaded("donors")
而不是:

'donors' => $this->getDonors(),
下面是我的get捐赠者()的一个片段

由于不存在“属于许多人”的组织,我必须建立一种定制的拉维关系,在这种关系中,我可以得到一个机构的所有捐助者。现在,我有两个问题

有没有一种方法可以让我动态加载getSenders()

有没有一种优雅的方式可以让我定制一个属于很多人的关系?这样我就可以

'donors' => $this->whenLoaded("donors")
我知道第三方软件包存在于许多关系中,但我想知道在您看来哪一个是最好的,因为我担心使用可能错误的软件包,最终会使我的系统更加糟糕


谢谢Laracasts

你有什么与这个问题相关的发现吗@Ignsd你对这个问题有什么发现吗@伊格尼斯
//  I am aware that this is wrong, I'd just like to give context if it's possible to do something like this
'donors' => $this->whenLoaded("donors", $this->getDonors()),
'donors' => $this->whenLoaded("donors")