2

i have 3 models that i want to start from a and load the relation with b and i want to load the relation betwen b and c there in a too is that possible ?? here is what i want to do in code :

AccommodationRoomModel which is the B model:
public function accommodation(){
        return $this->belongsTo(Accommodation::class);
    }

  public function roomPricingHistory(){
    return $this->hasMany(RoomPricingHistory::class);
}

and in The accomodation model :

public function accommodationRoom()
{
    return $this->Hasmany(AccommodationRoom::class);
}

and finaly in the room pricingHistory :

 public function accommodationRoom(){
    return $this->belongsTo(AccommodationRoom::class);
}

now in my accomodation controller i want to get All the accomodation with the room and from room i want to get the price so here is it

A = Accomodation
B = Room
C = price

and i want to call somehow like this

From A get B and The relation Of it with C and show all in A

2 Answers 2

2

You can use Laravels nested eager loading for this:

From the docs:

To eager load nested relationships, you may use "dot" syntax. For example, let's eager load all of the book's authors and all of the author's personal contacts in one Eloquent statement:

$books = App\Book::with('author.contacts')->get();

In your case:

$accomodations = Accomodation::with('accommodationRoom.roomPricingHistory')->get();
Sign up to request clarification or add additional context in comments.

Comments

1

nested-eager-loading

A::with('B.C');

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.