【问题标题】:error on Laravel Blade using foreach使用 foreach 的 Laravel Blade 错误
【发布时间】:2014-10-20 23:04:13
【问题描述】:

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

【问题讨论】:

  • 您确定是导致错误的确切 foreach 吗?你认为还有其他人吗?
  • 你能把你的控制器贴在你定义 $items 的地方吗?

标签: foreach laravel-4 blade


【解决方案1】:

试试:

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

改为。

【讨论】:

    猜你喜欢
    • 2015-07-10
    • 2019-06-26
    • 2018-03-07
    • 1970-01-01
    • 2014-08-08
    • 2016-04-27
    • 2016-04-14
    • 2018-04-27
    • 2021-09-05
    相关资源
    最近更新 更多