Php 如何将多维数组到集合转换为Laravel

Php 如何将多维数组到集合转换为Laravel,php,arrays,laravel,eloquent,Php,Arrays,Laravel,Eloquent,从API中,我得到了一个多维数组的响应,如: 如何将此数组转换为雄辩的对象,以便在视图中使用$collection->Query->Country或$collection->Carriers->where('id',12345)… 我尝试: $res1 = collect($res1)->map(function($row) { return collect($row); }); dd($res1); 但是没有成功 如何解决此问题?在这种情况下,您可以使用 $collecti

从API中,我得到了一个多维数组的响应,如:

如何将此数组转换为雄辩的对象,以便在视图中使用
$collection->Query->Country或$collection->Carriers->where('id',12345)…

我尝试:

$res1 = collect($res1)->map(function($row) {
    return collect($row);
});

dd($res1);
但是没有成功


如何解决此问题?

在这种情况下,您可以使用

$collection['Carriers']->where('id',12345)
// or
collect($collection['Carriers'])->where('id',12345)
$query =  (new Query())->setRawAttributes($collection['Query']);
为了使新的雄辩模型具有您可以使用的属性

$collection['Carriers']->where('id',12345)
// or
collect($collection['Carriers'])->where('id',12345)
$query =  (new Query())->setRawAttributes($collection['Query']);
然后使用

$query->Country;
或者你可以用这种粗糙的方法来做

$model = (new Model())->setRawAttributes($res1);
和你提到的用法

$model->Query->Country or $model->Carriers->where('id',12345)

写代码,不要放图像。我在arrayuse上调用了一个成员函数where()
collect($collection['Carriers'])->where('id',12345)