gpt4 book ai didi

PHP get_object_vars 不起作用

转载 作者:行者123 更新时间:2023-12-04 02:21:54 26 4
gpt4 key购买 nike

函数应该返回数组

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]=> 

最佳答案

get_object_vars无法正常工作,因为它只能找到实际(和可访问)的属性。一个 Collection (这是 $categories 是什么)没有可以读取的属性。您可能想做的是 lists()

$categories = Category::lists('category');
return $categories;

关于PHP get_object_vars 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28098072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com