2
votes

I have 2 tables bookings and eventDates.

bookings table has 2 fields checkIndate and checkOutDate which points to eventDates table.

I setup following relationships

public function eventCheckInDate() {
    return $this->belongsTo( 'EventDate', 'checkInDate' );
}
public function eventCheckOutDate() {
    return $this->belongsTo( 'EventDate', 'checkOutDate' );
}

How can I combine those relationship so that eloquent only runs single query and get the data?

What happens now is it run 2 queries even if checkInDate and checkOutDate are still the same.

Thank you

1
but why you are creating two methods for the same record I mean didn't it get the same row? what if we remove one method?Mahendra Pratap
@MahendraPratap because checkInDate and checkOutDate could be same or different for a booking.Mike Ross

1 Answers

0
votes

As I understand you need something like that. create another method for merging two relations.

public function getEventsInOutDateAttribute($value)
{
    $eventCheckInDate = $this->eventCheckInDate;
    $eventCheckOutDate = $this->eventCheckOutDate;

    // Merge collections and return single collection.
    return $eventCheckInDate->merge($eventCheckOutDate);
}