php - Eloquent (Laravel) 结果到数组

标签 php arrays laravel eloquent

我试图将 Eloquent (Laravel 4.2) 的结果获取到简单数组中,因此我使用 array_diff

在文档中,我找到了 all() 函数,它将结果提供给数组,但由于 array_diff,这些也是我无法拥有的东西。

我有:

$curCentres = Doccentre::where('r_document', $document)->select('r_id')->get()->all();

但这返回类似:

array(2) { 
    [0]=> object(Doccentre)#1125 (20) {
         ["table":protected]=> string(9) 
            "doccentre" ["primaryKey":protected]=> string(4) 
            "r_id" ["timestamps"]=> bool(false) 
            ["connection":protected]=> NULL 
            ["perPage":protected]=> int(15) 
            ["incrementing"]=> bool(true) 
            ["attributes":protected]=> array(1) {
                 ["r_id"]=> string(1) "1" 
            } 
            ["original":protected]=> array(1) { 
                ["r_id"]=> string(1) "1" 
            }
            ["relations":protected]=> array(0) { } 
            ["hidden":protected]=> array(0) { } 
            ["visible":protected]=> array(0) { } 
            ["appends":protected]=> array(0) { } 
            ["fillable":protected]=> array(0) { } 
            ["guarded":protected]=> array(1) { 
                [0]=> string(1) "*" 
            } 
            ["dates":protected]=> array(0) { } 
            ["touches":protected]=> array(0) { } 
            ["observables":protected]=> array(0) { } 
            ["with":protected]=> array(0) { } 
            ["morphClass":protected]=> NULL 
            ["exists"]=> bool(true) 
        } 
    [1]=> object(Doccentre)#1124 (20) { 
        ["table":protected]=> string(9) 
        "doccentre" ["primaryKey":protected]=> string(4) 
        ....
} 

我需要的是:

array(2) { [0]=> string(1) "1" [1]=> string(1) "2" } 

有什么办法可以得到吗?我也尝试过 toArray(),但它只会出错。

最佳答案

您可以使用lists来检索列值列表:

$curCentres = Doccentre::where('r_document', $document)->lists('r_id');

希望这有帮助。

关于php - Eloquent (Laravel) 结果到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35649386/

相关文章:

php - Composer : conflict with your requirements or minimum-stability

php - vagrant up 命令失败

java - 在java中使用多态数组

python - Numpy 数组 : concatenate arrays and integers

php - 在 laravel 中删除与用户相关的所有帖子

php - 在此页面中悬停不起作用,我不知道为什么,即使我已经给了它,页脚也没有占用 100% 的宽度

php - 整个 PHP 代码运行两次

javascript - 对象数组值的 JSON 数学运算

arrays - Laravelcollect($array)->sortBy($value)->values() 显示在屏幕上

php - 如何在 Laravel Eloquent 查询中收集项目?