I would like to be able to have an "Event Revenu" relationship.
Right now I can use $bookings->revenu(), but when I trying $event->bookings->revenu() it does not work.
# Event Model
- Id
- EventName
public function bookings()
{
return $this->hasMany( '\App\Booking', 'event_id' );
}
# Booking Model
- id
- EventID
public function event()
{
return $this->belongsTo('App\Event');
}
public function lines()
{
return $this->hasMany('App\BookingLine', 'booking_id');
}
public function revenu()
{
return $this->lines->sum('price') ;
}
# BookingLine Model
- Id
- BookingId
- Price
- Quantity
public function booking()
{
return $this->belongsTo('App\Booking', 'booking_id');
}
public function revenue()
{
return $this->hasManyThrough(
'App\BookingLine', // event has many lines
'App\Booking', // Through bookings
'event_id', // the Foreign key on Booking table that point to Model
'booking_id' // Foreign key on booking_lines table
);
}