I've got 4 different models. But don't quiet got the hang on the relationship I'm supposed to use. Models: County
, Municipality
, Postal
and Zip
. Table and primary keys are correctly set in each model.
In County
model I've added:
public function municipality() {
return $this -> hasMany('App\Municipality');
}
In Municipality
model I've added:
public function postal() {
return $this -> hasMany('App\Postal');
}
And in Postal
model I've added:
public function zip() {
return $this -> hasMany('App\Zip');
}
I have tested this: County::findOrFail(1)->municipality;
and it works. But how would I get the postals that belongs to that specific municipality?
I can't do this: County::findOrFail(1)->municipality->postal;
which I thought I could. But I can see I don't have the hang of it yet.
How would I do this? Thanks in advance.
Database structure: http://pastebin.com/ts0D5juq
You missed something:
County::findOrFail(1)->municipality;
Here you have the municipality
field of county not the actual object.
You have to use your functions:
County::findOrFail(1)->municipality()->postal();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.