从Laravel的EloquentCollection中获取特定的属性值



我正在尝试获取属性"日期"值从以下返回的数据,我怎么得到它?

IlluminateDatabaseEloquentCollection {#267 ▼
#items: array:1 [▼
0 => AppModelsCrisis_indicators_data {#255 ▼
#table: "crisis_indicators_data"
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:4 [▼
"c_i_data_id" => 1
"date" => "2022-05-23"
"indicator_id" => 30
"value" => 11
]
#original: array:4 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▶]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
]
}

我尝试了以下方法,但没有返回数据

$row_data_temp->date
$row_data_temp[0]->date
$row_data_temp[0]['date']

我不想使用for循环或foreach因为性能方面

//让你的主要集合与所有的属性像这样…

$users = Users::get();

//使用属性子集构建第二个集合。这个新//collection将是一个普通数组的集合,而不是Users模型的集合。

$subset = $users->map(function ($user) {
return $user->only(['id', 'name', 'email']);
});

stackoverflow裁判:这里

最新更新