samedi 18 février 2017

belongsToMany on not primary field

For example i have 4 tables:
buildings
id, title, ...
rooms
id, building_id, title, ...
companies
id, title, ...
companies_to_buildings
company_id, building_id

and i want to get companies from the room model.
From building model i can do something like

class Building extends Model
{
    public function companies(){
        return $this->belongsToMany('App\Company', 'company_building');
    }
}

or

class Building extends Model
{
    public function companies(){
        return $this->belongsToMany('App\Company', 'company_building', 'building_id', 'company_id');
    }
}

but if i do this in room model, it would not work. it will try to find rows in companies_to_buildings table where companies_to_buildings.building_id=room.id. how can i get companies from the room model?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire