php - Laravel Eloquent Get certain columns from nested tables using with() -
i'm trying table nested inheritance , filtered columns. , can't find ho easy it.
i have table shop witch has many locations (and location has 1 city) , many actions
i want of in once eloquent , filter specific columns.
this how filtering shop table don't know how filter tables locations , actions.
$this->shop->with('locations')->with('actions')->get(array('id','name','recommended','category_id'));
i need this:
$this->shop ->with('locations',location::with('city', city::get(array('id','name')))->get(array('id','name'))) ->with('actions', action::get(array('id','name')))->get(array('id','name')););
see laravel eloquent: how columns joined tables
the easiest way filter columns in model (as stated in top answer linked above). however, can inconvenient when want build dynamic queries.
theoretically should able do:
$this->shop->with(array('locations' => function($query) { $query->select('id', '...etc...'); }
...unfortunately doesn't seem work select(); when tested got blank array. other methods work, however, e.g.
$this->shop->with(array('locations' => function($query) { $query->orderby('id', 'desc'); }
you might better off using fluent query builder. isn't "sexy" or trendy orm, find easier use when dealing dynamic queries.
Comments
Post a Comment