【问题标题】:Load data from one model to another model, empty array. Laravel将数据从一个模型加载到另一个模型,空数组。拉拉维尔
【发布时间】:2015-07-02 18:52:37
【问题描述】:

我有 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() 并正常工作。

我该怎么做才能带我去营地?

【问题讨论】:

  • 没错 print_r($categorias->toArray());exit;获取数据使用函数 toArray()

标签: php arrays laravel model


【解决方案1】:

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

使用 toArray() 获取数据

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 1970-01-01
    相关资源
    最近更新 更多