laravel 仅在 Eloquent ORM 上的集合函数结果总是 return 空集合
laravel Collections's function only on Elequent ORM result always return empty collection
我只想要 return id
、name
和 pic_thumbnail
列 Eloquent ORM 集合结果。我在 get()
的结果上使用 only()
。但我总是得到空集合。
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()]);
}
日志,
[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 (
)
根据manual中的例子。看起来它只适用于单个集合,不适用于集合集合。
我认为您必须执行以下操作。
$results = $models->map(function($model) {
return $model->only(['id', 'name']);
});
我只想要 return id
、name
和 pic_thumbnail
列 Eloquent ORM 集合结果。我在 get()
的结果上使用 only()
。但我总是得到空集合。
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()]);
}
日志,
[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 (
)
根据manual中的例子。看起来它只适用于单个集合,不适用于集合集合。
我认为您必须执行以下操作。
$results = $models->map(function($model) {
return $model->only(['id', 'name']);
});