2014-08-27 73 views
0

在我看來,與laravel刀片使用下面的循環錯誤的Laravel刀片使用的foreach

@foreach ($items as $item)        
     <li class="dd-item" data-id="13"> 
      <div class="dd-handle">{{ $item->title}} 
      </div> 
     </li> 
@endforeach 

我得到

Invalid argument supplied for foreach() (View:...) 

如果我用dd($項目),我看不出坑對象

object(Illuminate\Database\Eloquent\Collection)[473] 
    protected 'items' => 
    array (size=1) 
     0 => 
     object(Td\Reports\Contents\Contents)[471] 
      protected 'table' => string 'posts' (length=5) 
      protected 'fillable' => 
      array (size=5) 
       0 => string 'type' (length=4) 
       1 => string 'title' (length=5) 
       2 => string 'slug' (length=4) 
       3 => string 'author' (length=6) 
       4 => string 'content' (length=7) 
      protected 'validationRules' => 
      array (size=3) 
       'title' => string 'required' (length=8) 
       'slug' => string 'required|unique:posts,id,<id>' (length=29) 
       'content' => string 'required' (length=8) 
      protected 'validator' => null 
      protected 'connection' => null 
      protected 'primaryKey' => string 'id' (length=2) 
      protected 'perPage' => int 15 
      public 'incrementing' => boolean true 
      public 'timestamps' => boolean true 
      protected 'attributes' => 
      array (size=14) 
       'id' => string '2' (length=1) 
       'title' => string 'home' (length=4) 
       'slug' => string 'home' (length=4) 
       'type' => string 'menu_item' (length=9) 
       'content' => string '' (length=0) 
       'parent' => string '0' (length=1) 
       'author' => string '0' (length=1) 
       'avatar' => string '' (length=0) 
       'guid' => string '' (length=0) 
       'mime_type' => string '' (length=0) 
       'menu_order' => string '0' (length=1) 
       'status' => string '0' (length=1) 
       'created_at' => string '0000-00-00 00:00:00' (length=19) 
       'updated_at' => string '0000-00-00 00:00:00' (length=19) 
      protected 'original' => 
      array (size=14) 
       'id' => string '2' (length=1) 
       'title' => string 'home' (length=4) 
       'slug' => string 'home' (length=4) 
       'type' => string 'menu_item' (length=9) 
       'content' => string '' (length=0) 
       'parent' => string '0' (length=1) 
       'author' => string '0' (length=1) 
       'avatar' => string '' (length=0) 
       'guid' => string '' (length=0) 
       'mime_type' => string '' (length=0) 
       'menu_order' => string '0' (length=1) 
       'status' => string '0' (length=1) 
       'created_at' => string '0000-00-00 00:00:00' (length=19) 
       'updated_at' => string '0000-00-00 00:00:00' (length=19) 
      protected 'relations' => 
      array (size=0) 
       empty 
      protected 'hidden' => 
      array (size=0) 
       empty 
      protected 'visible' => 
      array (size=0) 
       empty 
      protected 'appends' => 
      array (size=0) 
       empty 
      protected 'guarded' => 
      array (size=1) 
       0 => string '*' (length=1) 
      protected 'dates' => 
      array (size=0) 
       empty 
      protected 'touches' => 
      array (size=0) 
       empty 
      protected 'observables' => 
      array (size=0) 
       empty 
      protected 'with' => 
      array (size=0) 
       empty 
      protected 'morphClass' => null 
      public 'exists' => boolean true 

在控制器方法

public function getAll($child = null) { 

    $view = Request::segment(2); 

    $contents = array('menus', 'categories'); 
    if (in_array($view, $contents)) { 
     return View::make('reports::admin.' . $view . '.index') 
         ->with('items',$this->getMenus()); 
    } else { 
     return View::make('reports::admin.contents.index'); 
    } 
} 

/** 
* Get Menus 
* @return Posts 
*/ 
public function getMenus() { 
    //dd($this->model->where('type', 'menu_item')->get()); 
    return $this->model->where('type', 'menu_item')->get(); 
} 
+0

你確定這是導致錯誤的確切的foreach嗎?你認爲你有其他人嗎? – Laurence 2014-08-27 11:25:52

+0

您可以將您的控制器發佈到您定義$項目的位置嗎? – Laurence 2014-08-27 11:27:01

回答

0

嘗試:

return Response::make(View::make('reports::admin.' . $view . '.index', array('items'=>$this->getMenus()))); 

代替。