Laravel Eloquent使用with()从嵌套表中获取某些列

I'm trying to get table with nested inheritance and filtered for certain columns. And can't find ho to easy do it.

I have table shop witch has many locations (and location has one city) and many actions

I want to get all of it in once with Eloquent and filter for specific columns.

this how i was filtering shop table but don't know how to filter tables locations and actions.

$this->shop->with('locations')->with('actions')->get(array('id','name','recommended','category_id'));

I need something like 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 to get only certain columns from joined tables

The easiest way to do it is filter the columns in the model (as stated in the top answer linked above). However, that can be inconvenient when you want to build dynamic queries.

Theoretically you should be able to do:

$this->shop->with(array('locations' => function($query) {
                                            $query->select('id', '...etc...');
                                       }

...unfortunately it doesn't seem to work for select(); when I tested it I got a blank array. Other methods do work, however, e.g.

$this->shop->with(array('locations' => function($query) {
                                            $query->orderBy('id', 'DESC');
                                       }

You might be better off using Fluent Query Builder. It isn't as "sexy" or trendy as ORM, but I find it easier to use when dealing with dynamic queries.