samedi 1 août 2020

How to retrieve the data from table that are indirectly connected in laravel?

How can I get the room data from booking through roomAvailabilities table??

I tried using :

    $booking = Booking::all();
    $booking-> RoomAvailability;   //it works
    $booking -> Room;              //doesnot work

In booking model:

public function room()
{
    return $this->hasManyThrough('App\Room', 'App\RoomAvailability');
}

Sql Server query that might be similar to what I want:

SELECT * FROM dbo.Booking as b

INNER JOIN dbo.RoomAvailability as ra on b.bookingId = ra.BookingId

INNER JOIN dbo.Room as r on r.roomId = ra.roomId

Below is the sample of my table structure.

Booking:

id 
check_in_date
check_out_date
number_of_rooms

RoomAvailabilities:

id 
booking_id
room_id

Room:

id
room_number

Booking Model

public function roomAvailability()
{
    return $this->hasMany(RoomAvailability::class);
}

RoomAvailability Model

public function booking()
{
    return $this->belongsTo(Booking::class);
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire