将数据从一个模型加载到另一个模型,空数组。 Laravel

时间:2015-04-24 08:53:44

标签: php arrays laravel model

我有Post和Category模型。当我创建一个新帖子时,我希望在复选框类别上显示现有视线,但方法all()返回一个数组,该数组大小为表中现有类别的数量而没有任何数据。

    $categorias = Categoria::all();
    dd($categorias);

    return View::make('posts.nuevo')->with('categorias' => $categorias);

这是dd($ categorias)的内容:

object(Illuminate\Database\Eloquent\Collection)[218]
protected 'items' => 
array (size=7)
  0 => 
    object(Categoria)[209]
      public 'table' => string 'categorias' (length=10)
      public 'timestamps' => boolean false
      protected 'fillable' => 
        array (size=1)
          ...
      protected 'connection' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      protected 'attributes' => 
        array (size=2)
          ...
      protected 'original' => 
        array (size=2)
          ...
      ///// CONTINUE /////
    6 => 
    object(Categoria)[223]
      public 'table' => string 'categorias' (length=10)
      public 'timestamps' => boolean false

我在表格中插入了7行。

我有一个视图,显示其自己的模型类别的所有类别的列表,使用相同的方法all()并正常工作。

我怎么能把我带到难民营?

1 个答案:

答案 0 :(得分:5)

Model :: all()是正确的,你只需要从该数组中获取数据

  

使用toArray()获取数据

$categorias = Categoria::all();
print_r($categorias->toArray());exit;
return View::make('posts.nuevo')->with('categorias' => $categorias);
相关问题