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

似乎这里的教程无法按预期进行https://hackernoon.com/hiding-api-fields-dynamically-laravel-5-5-82744f1dd15a

我知道只有在必要时才使用“ 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()

如果Laravel凭着深厚的感情属于“属于很多人”,这一切都将非常容易,就像我本可以很容易使用的那样:

'donors' => $this->whenLoaded("donors")

代替:

'donors' => $this->getDonors(),

对于上下文,这是我的getDonors()的摘录

   $donors = [];
               // MBD -> Schedule -> Donation List -> Donor
               foreach($this->mbds as $mbd){
                    foreach($mbd->mbd_schedules as $schedule){
                         foreach($schedule->donation_list as $donation_list){
                              if(!in_array($donation_list->donation->donor,$donors)){
                                   array_push($donors,new UserResource($donation_list->donation->donor));
                              } 
                         }
                    } 
               } 
               return $donors;

由于不存在“属于很多人”,因此我必须创建一个自定义的laravel关系,从中可以获取代理机构的所有捐助者。现在,我有两个问题。

我是否可以动态加载getDonors()?

//  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")

我知道存在属于许多关系的第三方软件包,但是我想知道哪种软件包最适合您,因为我担心使用可能错误的软件包并最终使我的系统陷入困境更多。

谢谢Laracasts!

fanqi789 回答:隐藏API资源字段/通过深层关系问题属于许多人

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3162552.html

大家都在问