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
Post a Comment