I am looking for a way to do the following:
Let's say we have the Theater application, and we have three Models: Event , Session and Sitting , on the one hand, and Rate , on the other.
We have a morphToMany relationship, so Rate can be associated with any of the other three models.
Now we want to have a rate () relation in each model, which works in kind in a cascading mode. If we request bids () on the Seat instance, he will return his bids, if any, otherwise he will check whether the Session has tariffs and refund them. If not, he will refund the rates associated with the event.
So, we want to define as a hierarchical or cascading mode for extracting bets.
EDIT
Additional information : Moreover, the difficult part is, for example, an event, perhaps two Bets: General and Youth for $ 50 and $ 30 each. But this session may have General at $ 40. Thus, for this Session, the rates () method should return two bets: General at $ 40 and Youth at $ 30, since it was not "redefined" in the instance of the session.
If Session->rates()there is null, it is easy to return $this->event->rates(), but not so easy when I want to apply these "cascades" described earlier
EDIT 2
After my first approaches, I see strange behavior. To make it simple: I have a 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. The session has no bets and should be taken from its event.
2 1. 2 1.
Session::find(2)->rates(), Event1 , Session::find(2)->load('rates'), .
, :
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'
WHERE: WHERE assignated_rates. assignated_rate_id ('2'), 2 - , , .
, ? - ? , ID = 1,
?
!