查询构建器获取结果数组,而不是Laravel中的对象 [英] Query builder get array of results instead of objects in Laravel

查看:55
本文介绍了查询构建器获取结果数组,而不是Laravel中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的查询

DB::table('genres')
        ->join('albumGenres', 'genres.id', '=', 'albumGenres.genre_id')
        ->join('albums', 'albumGenres.album_id', '=', 'albums.id' )
        ->select('genres.id')
        ->where('albumGenres.album_id','=',$this->id)->get();

但是我得到这样的东西

"genre_id": [
        {
            "id": "4"
        },
        {
            "id": "8"
        }
    ]

我应该怎么做才能将结果作为一个数组

What should I do to get the results as just an array

"genre_id" : [4,8]

推荐答案

对于新版本的Laravel,您应该使用pluck()而不是list().

With new versions of Laravel, you should use pluck() instead of lists().

DB::table('genres')
->join('albumGenres', 'genres.id', '=', 'albumGenres.genre_id')
->join('albums', 'albumGenres.album_id', '=', 'albums.id' )
->where('albumGenres.album_id', '=', $this->id)
->pluck('genres.id');

以下是文档中的参考: https://laravel.com/docs/5.4/queries#retrieving-results (请参阅检索列值列表")

Here is the reference in the documentation: https://laravel.com/docs/5.4/queries#retrieving-results (see 'Retrieving A List Of Column Values')

这篇关于查询构建器获取结果数组,而不是Laravel中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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