I want only return the id
, name
and pic_thumbnail
column of the Eloquent ORM collections result. I use only()
on the result of get()
. But I always get the empty collection.
public function models()
{
if(!isset($_SESSION["uid"]))
return Response::json(['error' => 'please login.']);
$models = Model::where('user_id', $_SESSION["uid"])->get();
Log::info($models);
Log::info($models->only(['id', 'name']));
Log::info($models->only(['id', 'name'])->all());
foreach($models as $model){
$pic = $model->pics()->orderBy('displayorder')->first();
if($pic == null)
$model->pic_thumbnail = 'static/thumbnail_no_image.jpg';
else
$model->pic_thumbnail = $pic->imgfilename('thumbnail');
}
Log::info($models);
Log::info($models->only(['id', 'name', 'pic_thumbnail']));
Log::info($models->only(['id', 'name', 'pic_thumbnail'])->all());
return Response::json(['models' => $models->only(['id', 'name', 'pic_thumbnail'])->all()]);
}
Log,
[2017-11-13 11:29:34] local.INFO: [{"id":83,"name":"ssss","description":"sssssssssss","category":3,"internalcomment":"","rating_editor":"4","created_at":"2017-08-30 01:29:49","updated_at":"2017-08-30 01:29:50","deleted_at":null,"zipall_name":"ssss.zip","zipall_size":770146,"zipall_md5":"b10a0462e6010659ba210bb09487b42a","user_id":52,"price":null},{"id":105,"name":"asdfasad","description":"12","category":2,"internalcomment":"","rating_editor":"4","created_at":"2017-10-26 15:22:02","updated_at":"2017-10-26 15:22:02","deleted_at":null,"zipall_name":"asdfasad.zip","zipall_size":10545,"zipall_md5":"7f1b88e67ce98a6499f06d6d7cd3e530","user_id":52,"price":null},{"id":122,"name":"oooooooooo","description":"oooooooooo","category":2,"internalcomment":"","rating_editor":"4","created_at":"2017-11-03 10:17:08","updated_at":"2017-11-03 10:19:03","deleted_at":null,"zipall_name":"oooooooooo.zip","zipall_size":21242,"zipall_md5":"9177faf7ae2c4b7c12f9208a99690130","user_id":52,"price":0.01},{"id":123,"name":"skull","description":"skull demon","category":2,"internalcomment":"","rating_editor":"4","created_at":"2017-11-08 11:08:30","updated_at":"2017-11-08 11:08:30","deleted_at":null,"zipall_name":"skull.zip","zipall_size":299086,"zipall_md5":"22fe1047b13630ffcc0be115634ce167","user_id":52,"price":0}]
[2017-11-13 11:29:34] local.INFO: []
[2017-11-13 11:29:34] local.INFO: array (
)
[2017-11-13 11:29:34] local.INFO: [{"id":83,"name":"ssss","description":"sssssssssss","category":3,"internalcomment":"","rating_editor":"4","created_at":"2017-08-30 01:29:49","updated_at":"2017-08-30 01:29:50","deleted_at":null,"zipall_name":"ssss.zip","zipall_size":770146,"zipall_md5":"b10a0462e6010659ba210bb09487b42a","user_id":52,"price":null,"pic_thumbnail":"static\/thumbnail_no_image.jpg"},{"id":105,"name":"asdfasad","description":"12","category":2,"internalcomment":"","rating_editor":"4","created_at":"2017-10-26 15:22:02","updated_at":"2017-10-26 15:22:02","deleted_at":null,"zipall_name":"asdfasad.zip","zipall_size":10545,"zipall_md5":"7f1b88e67ce98a6499f06d6d7cd3e530","user_id":52,"price":null,"pic_thumbnail":"\/data\/pic\/thumbnail\/0\/183.jpg"},{"id":122,"name":"oooooooooo","description":"oooooooooo","category":2,"internalcomment":"","rating_editor":"4","created_at":"2017-11-03 10:17:08","updated_at":"2017-11-03 10:19:03","deleted_at":null,"zipall_name":"oooooooooo.zip","zipall_size":21242,"zipall_md5":"9177faf7ae2c4b7c12f9208a99690130","user_id":52,"price":0.01,"pic_thumbnail":"\/data\/pic\/thumbnail\/0\/229.jpg"},{"id":123,"name":"skull","description":"skull demon","category":2,"internalcomment":"","rating_editor":"4","created_at":"2017-11-08 11:08:30","updated_at":"2017-11-08 11:08:30","deleted_at":null,"zipall_name":"skull.zip","zipall_size":299086,"zipall_md5":"22fe1047b13630ffcc0be115634ce167","user_id":52,"price":0,"pic_thumbnail":"\/data\/pic\/thumbnail\/0\/231.jpg"}]
[2017-11-13 11:29:34] local.INFO: []
[2017-11-13 11:29:34] local.INFO: array (
)
Saumini Navaratnam
According to the example in the manual. it looks like it works only for single collection, not for collection of collections.
I think you have to do something like below.
$results = $models->map(function($model) {
return $model->only(['id', 'name']);
});
来源:https://stackoverflow.com/questions/47256777/laravel-collectionss-function-only-on-elequent-orm-result-always-return-empty-c