Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load data from one model to another model, empty array. Laravel

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.

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

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

This is the content of 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.

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

how I can do to take me the camps?

like image 589
relien Avatar asked Apr 24 '15 08:04

relien


1 Answers

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

Use toArray() to get Data

$categorias = Categoria::all();
print_r($categorias->toArray());exit;
return View::make('posts.nuevo')->with('categorias' => $categorias);
like image 81
kamlesh.bar Avatar answered Nov 11 '22 23:11

kamlesh.bar