函数应返回数组
public static function categories() {
// return array of categories
$categories = Category::select('category')->get();
return get_object_vars($categories);
}
然而,当我转储categories()
时,我得到array(0) { }
类别表
+----+----------------------+
| id | category |
+----+----------------------+
| 1 | Appliances |
| 2 | Auto Parts |
| 3 | Boats & Watercraft's |
| 4 | Campers |
+----+----------------------+
当我返回$categories
时,我会得到更长版本的
object(Illuminate\Database\Eloquent\Collection)#272 (1) { ["items":protected]=> array(20) { [0]=> object(Category)#289 (20) { ["table":protected]=> string(10) "categories" ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(1) { ["category"]=> string(10) "Appliances" } ["original":protected]=> array(1) { ["category"]=> string(10) "Appliances" } ["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(Category)#288 (20) { ["table":protected]=> string(10) "categories" ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(1) { ["category"]=> string(10) "Auto Parts" } ["original":protected]=> array(1) { ["category"]=> string(10) "Auto Parts" } ["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) } [2]=> object(Category)#271 (20) { ["table":protected]=>
答案 0 :(得分:3)
get_object_vars
无法正常工作,因为它只能找到实际(且可访问)的属性。 Collection
($categories
是什么)没有可以读取的属性。您可能想要做的是lists()
$categories = Category::lists('category');
return $categories;
答案 1 :(得分:0)
我认为您的财产不公开!
如果您查看手册,您会看到原因:http://php.net/manual/en/function.get-object-vars.php
德语引用(更清楚地说明):
LiefertdieöffentlichenElementeeines Objekts
翻译:(返回对象的公共元素)
这也是手册中如何获得所有财产的一个例子:
<?php
class foo {
private $a;
public $b = 1;
public $c;
private $d;
static $e;
public function test() {
var_dump(get_object_vars($this));
}
}
$test = new foo;
var_dump(get_object_vars($test));
$test->test();
?>
输出:
array(2) {
["b"]=>
int(1)
["c"]=>
NULL
}
array(4) {
["a"]=>
NULL
["b"]=>
int(1)
["c"]=>
NULL
["d"]=>
NULL
}
从手册示例中可以看出,您不会从类中获取任何非公开的属性,因此您可以在类中添加一个函数,该函数将返回每个属性