在Laravel 5 Collection中,如何返回一个对象数组而不是一个数组数组?

eba*_*nin 5 php iterator laravel laravel-5

我正在使用Laravel 5和Blade模板.在视图中,我想迭代一组Model对象,而不是一个数组数组.如果我确实想迭代一个数组数组,我会执行以下操作,它按预期工作:

$models = Foo::where('id', '>', 5)->get();
return view('home.index', ['models' => $models->toArray()]);
Run Code Online (Sandbox Code Playgroud)

但是,我想要一个具有可访问属性的对象数组.如果我要跑:

$models = Foo::where('id', '>', 5)->get();
return view('home.index', ['models' => $models->all()]);
Run Code Online (Sandbox Code Playgroud)

var_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
Run Code Online (Sandbox Code Playgroud)

不仅"物品"对象中的模型未填充属性.

在一个视图中我想做这样的事情:

@foreach ($models as $model)
    @include('_partial') {
        'id' => $model->id,
        'foo' => $model->foo,
    }
@endforeach
Run Code Online (Sandbox Code Playgroud)

如何获取模型数组而不是模型数组的数组?

小智 9

根据文件:

$array = $collection->all();
Run Code Online (Sandbox Code Playgroud)

"all方法返回集合表示的底层数组."


Bog*_*dan 5

您的代码很好,只是您不需要调用toArrayEloquent查询结果。让我解释一下代码的作用,这样您就可以理解为什么要这样:

$models = Foo::where('id', '>', 5)->get();
return view('home.index', ['models' => $models]);
Run Code Online (Sandbox Code Playgroud)

第一个statememt Foo::where('id', '>', 5)->get();返回类型的值Illuminate\Support\Collection

Collection类拥有被称为保护财产集合元素$items(如,你可以从你的转储看protected 'items' =>,这是类型)array。该类还实现了一个称为IteratorAggregate的接口,这基本上意味着它允许使用一条foreach语句来迭代该类型的任何变量。

在您的情况下,这意味着,即使$models类型Illuminate\Support\Collection为,当您使用foreach以下代码遍历它时,它也将表现为数组:

@foreach ($models as $model)
{
    {{ $model->foo }}
}
Run Code Online (Sandbox Code Playgroud)

简而言之,Collection是一个可迭代的对象,可以将其视为数组,但是比数组更好,因为if提供了允许您操作集合中项目的额外方法。您可以检查Collection API以查看可用方法的完整列表。

因此,实际上,您会得到一系列改进的模型。


另外,不要担心属性没有被填充,实际上它们是被填充的,我只是认为您在寻找错误的位置。

如果你仔细观察var_dump,你会发现你有一些这样开始的行publicprotectedprivate。这些关键字意味着这些行包含对象属性。对于Laravel Eloquent模型,从数据库获取的值不会直接存储在像数据库列这样命名的属性中。这些值实际上存储在名为的单个属性中attributes,并使用PHP的magic来获取_get。看看下面的代码注释:

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
Run Code Online (Sandbox Code Playgroud)

Laravel在幕后做了很多骗术,使您仅用几行代码就可以完成工作。这就是为什么a var_dump不会总是显示您期望的简单数据结构的原因。


eba*_*nin 0

找出问题所在了。我在模型中明确定义了属性。Laravel 以特定方式使用 __get() ,这会导致传递的参数被覆盖,无论显式定义的属性如何。

换句话说,我在部分中得到空值,因为我传递给部分的信息被覆盖。