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

时间:2022-09-25 09:10:10

I have the Post and Category models. When I create a new Post I want to take existing sight for display on a checkbox category, but the method all() returns an array the size of the number of existing categories in the table without any data.

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

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

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

This is the content of dd($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

I have 7 rows inserted into the table.

我有7行插入表中。

I have a view that displays the list of all categories of its own model category with the same method all() and working properly.

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

how I can do to take me the camps?

我怎么能带我去营地?

1 个解决方案

#1


4  

Model::all() is correct you just need to fetch data out of that Array

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

Use toArray() to get Data

使用toArray()来获取数据

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

#1


4  

Model::all() is correct you just need to fetch data out of that Array

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

Use toArray() to get Data

使用toArray()来获取数据

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