2014-10-02 77 views
0

我使用laravel 4,當我試圖創建一個菜單下拉房產4

試圖讓非對象的屬性,我得到這個錯誤(查看:/Applications/MAMP/htdocs/test/app/views/layouts/master.blade.php)(查看:/Applications/MAMP/htdocs/test/app/views/layouts/master.blade.php)

我看不到我要去哪裏錯了

我master.blade.php

<li class="dropdown"> 
     <a href="#" class="dropdown-toggle" data-toggle="dropdown">work<span class="caret"></span></a> 
     <ul class="dropdown-menu" role="menu"> 
      @foreach($dropdowns as $dropdown) 
       <li> 
        {{ $dropdown->title }} 
       </li> 
      @endforeach 
     </ul> 
</li> 

我的PageController

$currentPage = new Page($pages[0]->id); 
$dropdowns = $currentPage->getPagesSelectList("parent", "", ""); 
return View::make('index', compact('dropdowns')); 

我的頁面模型

public function getPagesSelectList($name, $default = "", $js = "", $flag = true) 
{ 
    $list = array(
       'name' => $name, 
       'default' => $default, 
       'js' => $js, 
       'pages' => $this->getSubPages($flag, $default) 
      ); 

    return (object)$list; 
} 

public function getSubPages($flag, $default, $parent = 0, $num = 1) 
{ 
    $pages = array(); 
    $pageSql = \Page::where('parent', '=', $parent)->orderBy('num', 'ASC')->get(); 
    foreach ($pageSql as $result) 
    { 
     $page = new Page($result->id); 
     $page->setFromDatabase(); 
     $page->default = $default; 
     $page->select = ''; 
     if($page->id == $default) 
      $page->select = $default; 
     $page->space = $num; 
     if(!empty($page)) 
      $pages[] = $page; 
     $children = $page->getSubPages($flag, $default, $page->id, $num + 1); 
     if(!empty($children)) 
      $pages[] = $children; 
    } 
    return array_flatten($pages); 
} 

回答

0

假設其他代碼工作正常,在刀片,而不是:

@foreach($dropdowns as $dropdown) 
<li> 
    {{ $dropdown->title }} 
</li> 
@endforeach 

你應該使用:

@foreach($dropdowns->pages as $dropdown) 
<li> 
    {{ $dropdown->title }} 
</li> 
@endforeach 

這是因爲在getPagesSelectList中,您將getSubPages的結果賦值給pages數組的索引,然後將數組轉換爲對象。

+0

是的工作。謝謝 – user3323889 2014-10-02 13:11:48