使用foreach在Laravel Blade上出错

时间:2014-08-27 11:21:18

标签: foreach laravel-4 blade

在我的使用laravel blade的视图中使用以下循环

 @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($ items),我会看到洞对象

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();
}

1 个答案:

答案 0 :(得分:0)

尝试:

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

代替。