There is a table of goods and photos. These tables are interconnected by a one-to-many relationship. That is, one good can have a lot of photo. I need to make a selection of all the good, immediately along with with all related photo. You also need to select the values ​​of not all fields, but only the necessary ones. And this must be done both in the goods and in the photographs. You need to get this result:

array:1 [ 0 => array:3 [ "id" => 22997 "name" => "Дроссельная заслонка, TOYOTA, -, 2AZ, Конт., (механич.)" "photos" => array:2 [ 0 => array:8 [ "file_name_1920" => "1920_beab058e9f965a26ca597c3a2d5fad85.jpg" ] 1 => array:8 [ "id" => 14 "file_name_1920" => "1920_ae27f461ea19678d07151993e6a30811.jpg"" ] ] ] ] 

I do a sample like this:

 $goods = Good::where('quantity', '>', 0) ->select( [ 'id', 'name', ]) ->with(['photos' => function($q) { $q->select('id', 'file_name_1920'); }]) ->orderBy('id', 'desk')->take(1)->get(); dump($goods->toArray()); 

As a result, I get a sample without photos:

 array:1 [ 0 => array:3 [ "id" => 22997 "name" => "Дроссельная заслонка, TOYOTA, -, 2AZ, Конт., (механич.)" "photos" => [] ] ] 

And if I do a sample like this:

 $goods = Good::where('quantity', '>', 0) ->select( [ 'id', 'name', ]) ->with(['photos' => function($q) { //$q->select('id', 'file_name_1920'); }]) ->orderBy('id', 'desk')->take(1)->get(); dump($goods->toArray()); 

I get everything with good, I get all the photos, but with unnecessary fields:

 array:1 [ 0 => array:3 [ "id" => 22997 "name" => "Дроссельная заслонка, TOYOTA, -, 2AZ, Конт., (механич.)" "photos" => array:2 [ 0 => array:8 [ "id" => 13 "sorting" => 0 "good_id" => 22997 "publish" => 1 "file_name_1920" => "1920_beab058e9f965a26ca597c3a2d5fad85.jpg" "file_name_fit_640" => "fit_640_beab058e9f965a26ca597c3a2d5fad85.jpg" "created_at" => "2016-10-19 18:23:47" "updated_at" => "2016-10-19 18:23:47" ] 1 => array:8 [ "id" => 14 "sorting" => 0 "good_id" => 22997 "publish" => 1 "file_name_1920" => "1920_ae27f461ea19678d07151993e6a30811.jpg" "file_name_fit_640" => "fit_640_ae27f461ea19678d07151993e6a30811.jpg" "created_at" => "2016-10-19 18:23:49" "updated_at" => "2016-10-19 18:23:49" ] ] ] ] 

What am I doing wrong?

3 answers 3

  ->with(['photos' => function($q) { $q->select('id', 'file_name_1920'); }]) 

everything is correct, just the fields for which there is a connection must also be present in the sample.

    On a simple example:
    $ model = App \ Model :: find (34); // get model by id 34

     // Если нужно массивом из основной модели $array = $model->only('id', 'created_at', 'updated_at'); // Если нужно коллекцию но с нужными полями $collection = $model->select('id', 'created_at', 'updated_at')->get(); // Если нужна связь с нужными ячейками $relation = $model->relationModel->pluck('id'); 

      In one answer, described how to correctly use with : Laravel: how to limit eager loading (eager-loading)?

       Model::with(array(bla-bla)) // with всегда в начале запроса ->where(bla-bla) ->get(); 

      or

       Model::where(bla-bla) ->get() ->load(array(bla-bla)); // load применяется к коллекции (после get()) 

      May solve your question