jeudi 20 août 2015

How to define a hasMany far relationship

Suppose you have

Facility  1------*  Section  1------*  Session  *------1  Event

I.e., a facility has many sections; each section can hold many sessions; each session belongs to an event.

How can I define this as a relationship in the Facility model to retrieve all unique instances of Event that a facility is hosting? I have tried this:

class Facility extends Eloquent\Model {
    public function events() {
        return $this->hasMany('Event')
            ->join('session', 'session.event_id', '=', 'event.id')
            ->join('section', 'section.id', '=', 'session.section_id')
            ->join('facility', 'facility.id', '=', 'section.facility_id');
    }
}

I don't know if I'm very close with that; Laravel adds a constraint implicitly ("events"."facility_id" in (...)) and everything gets messed up.

What is the proper way of doing this?

laravel php laravel 5 laravel 4 laravel with laravel tutorial

Aucun commentaire:

Enregistrer un commentaire