laravel Collections仅在EORquent ORM结果上的函数总是返回空集合 [英] laravel Collections's function only on Elequent ORM result always return empty collection

查看:104
本文介绍了laravel Collections仅在EORquent ORM结果上的函数总是返回空集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想返回雄辩的ORM收集结果的idnamepic_thumbnail列.我在get()的结果上使用only().但是我总是得到空的收藏集.

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()]);
}

日志

[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 (
)  

推荐答案

根据

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']);
});

这篇关于laravel Collections仅在EORquent ORM结果上的函数总是返回空集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆