dimanche 27 novembre 2016

Laravel: get children by parent slug

I have a Section and a Picture models, with a one-to-many relationship (Section can have many Pictures; Picture has only one Section)

I'm able to retrieve all Pictures given a Section id:

$section = '1';
$records = Picture::where('section_id', $section)->orderBy('position')->get();

What if I'd like to retrieve Picture by Section slug (or name)? All these examples don't work:

$section = 'wedding';
$records = Picture::where('sections.slug', $section)->orderBy('position')->get(); // nope
$records = Picture::where($this->section()->slug, $section)->orderBy('position')->get(); // nope

I tried to search in Laravel docs, but I didn't get the case... Thank you



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire