我试图将 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/