Syntax for calling child relationship with a condition on the parent in Laravel -


i have model called 'tag' has self-referencing 'hasmany' relationship on itself. example have tag called uk , 'has many' child tags called london, liverpool, manchester etc.

here relationship in tag model file:

public function children() {     return $this->hasmany('app\tag', 'tag_parent'); } 

i want list of child cities parent 'uk' tag. have code:

$cities = tag::where('title', 'uk')->get()->children; 

i getting error saying children unknown. docs should works expected.

$cities = tag::find($id)->children; 

so how list of child cities 'where' condition? thought tag::find($id) shortcut tag::where('id', $id)->get()?

update

exact error:

errorexception in tagcontroller.php line 28: undefined property: illuminate\database\eloquent\collection::$children 

as @revo mentioned, use with method. it's called eager loading in laravel.

tag::with('children')->where('title', 'uk')->get(); 

this should gives tags title = uk, plus children that's related each of every individual tags.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -