I am looking a way of doing the following:
Let's say we have a Theater application and we have three Models: Event, Session and Seat on one hand and Rate on the other hand.
We have a morphToMany relation so a Rate can be associated with any of the other three models.
Now, we want to have a rates() relation in each model that work in kind a cascade mode. If we ask for rates() on a Seat instance it will returns its Rates if the have, otherwise, it will check if the Session does have Rates and will return them. If not, it will return the Rates associated with Event.
So we want to define like a hierachy or cascade mode to retrieve Rates.
EDIT
More information: Moreover, the tricky part is, for example, an Event may have two Rates: General and Youth for 50$ and 30$ each. But a given session maybe has General at 40$. So for this given Session, the rates() method should return two rates: General at 40$ and Youth at 30$ since it was not "overriden" in Session instance.
If Session->rates() is null it's easy to return $this->event->rates() but not so easy when I want to apply this "cascades" explained before
EDIT 2
After my first approaches I see some strange behaviour. To make it simple: I have class
<?php
namespace App;
use Backpack\CRUD\CrudTrait;
use Backpack\CRUD\ModelTraits\SpatieTranslatable\HasTranslations;
use Illuminate\Database\Eloquent\Model;
class Event extends Model
{
public function rates()
{
return $this->morphToMany('App\Rate', 'assignated_rate', 'assignated_rates', 'assignated_rate_id', 'rate_id')
->withPivot(['price', 'max_on_sale', 'max_per_inscription_set']);
}
}
and
<?php
namespace App;
use Backpack\CRUD\CrudTrait;
use Backpack\CRUD\ModelTraits\SpatieTranslatable\HasTranslations;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Session extends Model
{
public function event()
{
return $this->belongsTo('App\Event');
}
public function rates()
{
return $this->event->rates();
}
}
So I am trying the easiest situation. Session has no Rates and must be taken from its Event.
I have a Session with ID 2 and an Event with ID 1. Session 2 belongs to Event 1.
When I do Session::find(2)->rates() rates of Event1 is returned but when I do Session::find(2)->load('rates') nothing is returned.
If I explore the Query executed I got:
SELECT `rates`.*, `assignated_rates`.`assignated_rate_id` as `pivot_assignated_rate_id`, [.....] FROM `rates` inner join `assignated_rates` on `rates`.`id` = `assignated_rates`.`rate_id` WHERE `assignated_rates`.`assignated_rate_id` in ('2') and `assignated_rates`.`assignated_rate_type` = 'App\\Event'
Note the WHERE condition: WHERE assignated_rates.assignated_rate_id in ('2') where 2 is the ID of the Session, not the Event related to Session.
May it be a bug? Or am I missing something? I guess the condition should be bounded to ID=1 since it is Event's ID
How would you do this??
Thanks!