Php 在Laravel 5集合中,如何返回对象数组而不是数组数组?
我使用的是Laravel 5和刀片模板。在视图中,我希望迭代模型对象数组,而不是数组数组。如果我确实想在数组数组上迭代,我将执行以下操作,这与预期的一样有效:Php 在Laravel 5集合中,如何返回对象数组而不是数组数组?,php,laravel,iterator,laravel-5,Php,Laravel,Iterator,Laravel 5,我使用的是Laravel 5和刀片模板。在视图中,我希望迭代模型对象数组,而不是数组数组。如果我确实想在数组数组上迭代,我将执行以下操作,这与预期的一样有效: $models = Foo::where('id', '>', 5)->get(); return view('home.index', ['models' => $models->toArray()]); 但是,我需要一个具有可访问属性的对象数组。如果我要跑: $models = Foo::where('id'
$models = Foo::where('id', '>', 5)->get();
return view('home.index', ['models' => $models->toArray()]);
但是,我需要一个具有可访问属性的对象数组。如果我要跑:
$models = Foo::where('id', '>', 5)->get();
return view('home.index', ['models' => $models->all()]);
var\u dump
如下所示:
object(Illuminate\Support\Collection)[164]
protected 'items' =>
array (size=3)
0 =>
object(App\Foo)[172]
public 'id' => null
public 'foo' => null
private 'created_at' => null
private 'updated_at' => null
protected 'connection' => null
protected 'table' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=4)
'id' => int 1
'foo' => string 'Foo!' (length=4)
'created_at' => string '2015-02-27 15:44:09' (length=19)
'updated_at' => null
@foreach ($models as $model)
@include('_partial') {
'id' => $model->id,
'foo' => $model->foo,
}
@endforeach
模型不仅位于“items”对象中,而且属性未填充
从某种意义上说,我想这样做:
object(Illuminate\Support\Collection)[164]
protected 'items' =>
array (size=3)
0 =>
object(App\Foo)[172]
public 'id' => null
public 'foo' => null
private 'created_at' => null
private 'updated_at' => null
protected 'connection' => null
protected 'table' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=4)
'id' => int 1
'foo' => string 'Foo!' (length=4)
'created_at' => string '2015-02-27 15:44:09' (length=19)
'updated_at' => null
@foreach ($models as $model)
@include('_partial') {
'id' => $model->id,
'foo' => $model->foo,
}
@endforeach
如何获得一个模型数组而不是一个模型数组?您的代码很好,只是您不需要对雄辩的查询结果调用
toArray
。让我解释一下代码的作用,这样您就可以理解为什么下面是您想要的:
$models = Foo::where('id', '>', 5)->get();
return view('home.index', ['models' => $models]);
第一个statemtFoo::where('id','>',5)->get()代码>返回类型为照亮\Support\Collection
的值
该Collection
类在名为$items
的受保护属性中保存集合元素(从转储protected'items'=>
中可以看到),该属性的类型为array
。该类还实现了一个名为的接口,这基本上意味着它允许使用foreach
语句迭代该类型的任何变量
在您的情况下,这意味着,即使$models
属于illighted\Support\Collection
类型,当您使用foreach
查看它时,它也将作为一个数组:
@foreach ($models as $model)
{
{{ $model->foo }}
}
因此,简而言之,Collection
是一个iterable对象,可以将其视为数组,但优于数组,因为if提供了额外的方法,允许您操作集合中的项。您可以检查以查看可用方法的完整列表
所以在现实中,你得到了一系列改进的模型
另外,不要担心这些属性没有填充,它们实际上已经填充了,我只是觉得你找错地方了
如果仔细查看var\u dump
,您会发现有些行以public
、protected
或private
开头。这些关键字意味着这些行包含。对于Laravel Eloquent模型,从数据库中获取的值不会直接存储在与数据库列类似的属性中。这些值实际上存储在一个名为attributes
的属性中,并使用PHP的魔力获取。请看下面关于代码的注释:
object(Illuminate\Support\Collection)[164]
protected 'items' =>
array (size=3)
0 =>
object(App\Foo)[172]
public 'id' => null // <<< THE VALUES ARE
public 'foo' => null // <<< NOT STORED HERE
private 'created_at' => null
private 'updated_at' => null
protected 'connection' => null
protected 'table' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=4)
'id' => int 1 // <<< THEY ARE HERE
'foo' => string 'Foo!' (length=4) // <<< AND HERE
'created_at' => string '2015-02-27 15:44:09' (length=19)
'updated_at' => null
对象(照亮\支持\收集)[164]
受保护的“项目”=>
数组(大小=3)
0 =>
对象(App\Foo)[172]
公共“id”=>null//null
受保护的“表”=>null
受保护的“primaryKey”=>字符串“id”(长度=2)
受保护的“每页”=>int 15
公共“递增”=>布尔值true
公共“时间戳”=>布尔值true
受保护的“属性”=>
数组(大小=4)
“id”=>int1/解决了这个问题。我明确定义了模型中的属性。Laravel以一种特殊的方式使用_get(),这会导致传递的参数被显式定义的任何属性覆盖
换句话说,我在分部中得到空值,因为我传递给分部的信息被覆盖。根据:
“all方法返回集合表示的基础数组。”不要对$models调用toArray()。@我不是在调用toArray()
。这只是一个例子,如果我想要一个不同的结果,它会起作用。我不能理解你想说的。正如Bogdan所回答的,只需将(模型的)集合传递给视图就足够了;不需要在查询结束时调用toArray()。这里的问题是属性返回空值。换句话说,$model->id
和$model->foo
将始终返回null,如上面的var\u dump
所示。我将在一分钟内更新我的答案,解释为什么不是这样。即使我使用$model->attributes
我仍然依赖数组。我想要的是始终拥有$model->id
,而不是$model['id']
或$model->attributes['id']
。magic getter似乎没有返回所需的值。也许我从根本上误解了什么。你可以使用$model->id
。当您编写$model->id
时,model类确定您想要列id
的值,并且知道如何从属性数组中查找并返回它。它使用我在答案中引用的PHP方法来实现这一点。您的代码将按您的需要工作,因为Laravel在幕后发挥了它的魔力。此外,要将数据传递给Blade中的部分视图,您需要将其作为数组类型的第二个参数传递,如下所示:@include(“\u partial”“,['id'=>$model->id,'foo'=>$model->foo])
。这是正确的答案。谢谢谢谢@伊巴库宁请将此项标记为正确答案