1

I know I can load nested relationship in Laravel by a simple dot but I need to know if it's possible to load nested in resource and use it in API something like below:

  public function toArray($request)
    {
return [
     'Room' => AccommodationRoomResource::collection($this->firstrelation.NetstedRelation),
 ];

and my second question is that can I just load some certain fields of it as I don't need all the data to be loaded and make it so messy

1 Answer 1

1

What you need is Eager Loading. There is a documentation for that. Refer to Eager Loading By Default section.

In a nutshell, you should add $with property to your model:

...
protected $with = ['room']; /// given that relation name is 'room'
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.