2017-04-19 87 views
-6

這裏是我的看法,我不知道爲什麼我的嵌套德路不能正常工作,請幫助我爲什麼嵌套循環中laravel不工作

  <ul> 
    @foreach($users as $m) 
       <li> {{$m->namecategory}} 

        <ul> 
        @foreach($m->namesubcategory as $nam) 
          <li>{{$nam->namesubcategory}} 

          <ul> 
           @foreach($nam->namesubling as $lan) 
            <li>{{$lan->namesubling}}</li> 

           @endforeach 
          </ul> 


          </li> 
         @endforeach 
        </ul> 

       </li> 

    @endforeach 
     </ul> 

其投擲的錯誤像提供的foreach這個

參數無效()(查看

這裏使用輸出DD($用戶)

array:3 [▼ 
    0 => {#209 ▼ 
    +"idcategory": 1 
    +"namecategory": "mobile and assces" 
    +"created_at": null 
    +"updated_at": null 
    +"idsubcategory": 1 
    +"namesubcategory": "mobile" 
    +"idcategory_mastercategory": 1 
    +"idsubling": 1 
    +"namesubling": "iphone" 
    +"idsubcategory_subcategory": 1 
    } 
    1 => {#211 ▼ 
    +"idcategory": 1 
    +"namecategory": "mobile and assces" 
    +"created_at": null 
    +"updated_at": null 
    +"idsubcategory": 2 
    +"namesubcategory": "mobile cover" 
    +"idcategory_mastercategory": 1 
    +"idsubling": 2 
    +"namesubling": "nexu cover" 
    +"idsubcategory_subcategory": 2 
    } 
    2 => {#212 ▼ 
    +"idcategory": 2 
    +"namecategory": "elect" 
    +"created_at": null 
    +"updated_at": null 
    +"idsubcategory": 3 
    +"namesubcategory": "lap" 
    +"idcategory_mastercategory": 2 
    +"idsubling": 3 
    +"namesubling": "hp" 
    +"idsubcategory_subcategory": 3 
    } 
] 

和IM嘗試實現這一

 -mobile and accessory (mastercategory) 
     1.mobile(subcategory) 
       1.iphone(subling) 
     2.mobile cover(subcategory) 
       1.iphone cover(subling) 

-electronic(mastercategory) 
      1.laptop(subcategory) 
       1.hp(subling) 
+2

因爲'sub'不'$ M'存在嗎? –

+0

可以打印$ users數組來檢查數組中是否存在問題 – manian

+1

歡迎使用Stack Overflow!請[參觀],看看周圍,並通讀[幫助],尤其是[*我如何問一個好問題?](/幫助/如何問)沒有足夠的上面的細節能夠有效地幫助你。 –

回答

0

根據您的var_dump,$ M-> namesubcategory - 不是一個數組。 $ m-> namesubcategory,$ nam-> namesubling不是很好。 你不能對他們使用「foreach」。

+0

任何其他解決方案如何我可以使用嵌套爲每個或任何其他方法 –

+0

你能寫HTML代碼,你想要得到 – bahek2462774

+0

我已經寫了HTML代碼,我試圖實現以上是有任何其他的方法或者說我可以按照實現這一 –

0

你必須從你的集合中創建另一個數組。 然後你將能夠得到你需要的任何東西。

$result = []; 
foreach ($users as $u) { 
    $result[ $u-> namecategory ] = $result[ $u-> namecategory ] ?? []; 
    $result[ $u-> namecategory ][$namesubcategory] = $result[ $u-> namecategory ][$namesubcategory] ?? []; 
    $result[ $u-> namecategory ][$namesubcategory][] = $u-> namesubling; 
} 

在結果你將不得不像

result [mobile and assces] 
      [mobile] 
       [iphone] 
      [mobile cover] 
       [nexu cover] 

數組然後,你就可以「的foreach」這陣像你想。

請注意,即大成 「$ A = $ B [ 'XX']?[];」 - PHP7