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?
->select(['table_name.id', '...'])- Zippbl4