2016-12-18 182 views
0

我不確切知道這是如何工作的,所以這就是我要問的原因。我仍在製作一個公告牌系統,現在我想要在每個類別子類別下。爲laravel中的foreach()提供的無效參數5.3

例如: https://laracasts.com/discuss/channels/eloquent/eloquent-getting-subcategories-from-the-categories

現在我的IndexController看起來是這樣的: enter image description here

我已經使用上LaraCasts以下螺紋的做到了這一點

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use App\Thread; 
use App\Chat; 
use App\Category; 


class IndexController extends Controller 
{ 
    /** 
    * Show the application dashboard. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function index() 
    { 
     $threads = Thread::all()->toArray(); 
     $chats = Chat::orderBy('id', 'DESC')->take(50)->with('author')->get(); 
     $categories = Category::with('sub_category')->get(); 

     return view('index', compact('threads','chats','categories')); 
    } 

} 

這是我的分類型號:

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Category extends Model 
{ 


    public function sub_category() 
    { 
     return $this->belongsTo(self::class, 'parent_id', 'id'); 
    } 

    public function parent_category() 
    { 
     return $this->hasMany(self::class, 'id', 'parent_id'); 
    } 


} 

我的觀點(index.blade.php):

@foreach($categories as $category) 
     <div class="col-md-8"> 
     <div class="panel panel-primary"> 
      <div class="panel-heading"><i class="fa fa-angle-right" aria-hidden="true"></i> {{ $category->caption }}</div> 
      <div class="panel-body"> 



     @foreach($category->sub_category as $sub_category) 

       <div class="row forum-row"> 

        <div class="col-xs-12 col-md-8 col-sm-8"> 

        <div class="hidden-xs visible-sm visible-lg visible-md pull-left forum-icon-read"> 
        <i class="fa fa-comment"></i> 
        </div> 
        <a href="#">{{ $sub_category->caption }}</a><br> 
        <small>{{ $sub_category->desc }}</small> 

        </div> 

        <div class="col-xs-12 col-md-4 col-sm-4 forum-latest-container"> 
        <p class="forum-data cutoff"><a href="#">Test</a></p> 
        <small> 
        door <a class="Donateur" href="/user-Sygun">Sygun</a> 
        <p class="forum-data"> 
        2 minuten geleden </p> 
        </small> 
        </div> 

       </div> 
       @endforeach 


      </div> 
     </div> 
     </div> 
     @endforeach 

我也有兩個數據庫表:

  • - 行業標準(ID,標題,PARENT_ID,min_rank,DESC)

  • 類別(id,caption,min_rank,desc)

該錯誤消息我得到:提供的foreach

無效的參數()

你能告訴我什麼是實現這一目標的正確方法嗎?

+0

你在數據庫的「子類別」和「類別」中有兩個表嗎? –

+0

是的,我有@AmitGupta – Sygun

+0

'$ category-> sub_category'是一個數組嗎? (通過'var_dump($ category-> sub_category);'在內部循環之前驗證) - 也許你只是想循環'$ category'而不是? – Qirel

回答

0

創建兩個模型,分別命名爲CategorySubCategory

在分類模型

public function subcategories() 
{ 
    return $this->hasMany('App\SubCategory', 'parent_id'); 
} 

在子類別模型

public function parent_category() 
{ 
    return $this->belongsTo('App\Category', 'parent_id'); 
} 

然後在你的控制器查詢它像:

$categories = Category::with('subcategories')->get(); 

並希望您foreach會工作。

+0

不幸的是,我仍然得到相同的錯誤。你認爲數據庫結構應該如何?那麼表格的名稱應該如何改變,並且我必須改變列名稱中的內容? – Sygun

+0

'dd($ categories)'並顯示您的輸出。 –

+0

我必須在哪裏放置該功能?在哪個文件中? @AmitGupta – Sygun

0

分類模型

public function subcategories(){ return $this->hasMany('SubCategory', 'parent_id'); }

上查看

@foreach($categories as $category) @foreach($category->subcategories as $subcategory) {{$subcategory->caption}} @endforeach @endforeach

並確保您創建子類別的模型。

相關問題