mardi 2 février 2016

Laravel 5 eloquent relationship between 4 tables

I have four tables, i used Laravel 5.2 eloquent method

Municipalities
id
name
---------------
Barangays
id
name
municipality_id
----------------
Puroks
id
name
barangay_id
----------------
Households
id
name
purok_id

I have attached also hasMany(), belongsTo() relationship respectively in each model.

So that:

Municipalities hasMany() Barangays hasMany() Puroks hasMany() Households

and

Households belongsTo() Puroks belongsTo() Barangays belongsTo() Municipalities

I want to know is there a way to get the name of the municipality through Household query?

like

  query = Household->purok->barangay->municipality->name

And I will have little query to get the name of household, purok, barangay and municipality in one query.

I usually do it with manually leftjoin these tables.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire