简体   繁体   中英

Nested Mapped Laravel Query with Relationships

I am outputting a query which has a relationship with another model.

One of the fields I need is within the relationship

  $officeFlagMap = $officeFlagQuery->map(function ($item) {
     return [
        'propagent_id'   => $propagent_id,
     ];
     $item->theAgent->map(function ($inner){
        return [
           'agtFullName'   => $inner->agtFullName,
        ];
     });
  });

I have checked that both values are correct but I am only getting the output for the $inner->agtFullName.
How can I modify this to chain the returns and show both fields?

an ideal code would be,

$officeFlagMap = $officeFlagQuery->map(function ($item) {

  return [
        'propagent_id' => $item->propagent_id, // use the appropriate variable here 
        'agtFullName'  => $item->theAgent->map(function ($inner){
                              return [
                                'agtFullName'   => $inner->agtFullName,
                               ];
                           });
  ];
});

this will map over the original collection and again on the theAgent collection and will return the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM