2015-09-05 118 views
1

我有這樣的查看Laravel 5.1 - 嵌套循環 - 刀片模板

.. 
... 
<div class="section group"> 
    @foreach ($product as $item) 
    <div class="grid_1_of_4 images_1_of_4"> 
     <h4><a href="#">{!! $item->name !!}</a></h4> 
     <div class="price-details"> 
      <div class="price-number"> 
       <p><span class="price">$ {!! $item->price !!} </span></p> 
      </div> 
      <div class="add-cart">        
       <h4><a href="#">More Info</a></h4> 
      </div> 
      <div class="clear"></div> 
     </div>       
    </div>   
    @endforeach 
</div> 
... 
.. 

這對控制器

.. 
... 
public function index(){ 
    $product = product::paginate(8); 
    return view('shop.index', compact('product')); 
} 
... 
.. 

我想展現給數據我將控制器分成兩部分,輸出如下:

<div class="section group"> 
    <div class="grid_1_of_4 images_1_of_4"> 
     <h4><a href="#">item 01</a></h4> 
     <div class="price-details"> 
      <div class="price-number"> 
       <p><span class="price">$ price 01 </span></p> 
      </div> 
      <div class="add-cart">        
       <h4><a href="#">More Info</a></h4> 
      </div> 
      <div class="clear"></div> 
     </div>       
    </div> 
    <div class="grid_1_of_4 images_1_of_4"> 
     <h4><a href="#">item 02</a></h4> 
     <div class="price-details"> 
      <div class="price-number"> 
       <p><span class="price">$ price 02 </span></p> 
      </div> 
      <div class="add-cart">        
       <h4><a href="#">More Info</a></h4> 
      </div> 
      <div class="clear"></div> 
     </div>       
    </div> 
    <div class="grid_1_of_4 images_1_of_4"> 
     <h4><a href="#">item 03</a></h4> 
     <div class="price-details"> 
      <div class="price-number"> 
       <p><span class="price">$ price 03 </span></p> 
      </div> 
      <div class="add-cart">        
       <h4><a href="#">More Info</a></h4> 
      </div> 
      <div class="clear"></div> 
     </div>       
    </div> 
    <div class="grid_1_of_4 images_1_of_4"> 
     <h4><a href="#">item 04</a></h4> 
     <div class="price-details"> 
      <div class="price-number"> 
       <p><span class="price">$ price 04 </span></p> 
      </div> 
      <div class="add-cart">        
       <h4><a href="#">More Info</a></h4> 
      </div> 
      <div class="clear"></div> 
     </div>       
    </div> 
</div> 

<div class="section group"> 
    <div class="grid_1_of_4 images_1_of_4"> 
     <h4><a href="#">item 05</a></h4> 
     <div class="price-details"> 
      <div class="price-number"> 
       <p><span class="price">$ price 05 </span></p> 
      </div> 
      <div class="add-cart">        
       <h4><a href="#">More Info</a></h4> 
      </div> 
      <div class="clear"></div> 
     </div>       
    </div> 
    <div class="grid_1_of_4 images_1_of_4"> 
     <h4><a href="#">item 06</a></h4> 
     <div class="price-details"> 
      <div class="price-number"> 
       <p><span class="price">$ price 06 </span></p> 
      </div> 
      <div class="add-cart">        
       <h4><a href="#">More Info</a></h4> 
      </div> 
      <div class="clear"></div> 
     </div>       
    </div> 
    <div class="grid_1_of_4 images_1_of_4"> 
     <h4><a href="#">item 07</a></h4> 
     <div class="price-details"> 
      <div class="price-number"> 
       <p><span class="price">$ price 07 </span></p> 
      </div> 
      <div class="add-cart">        
       <h4><a href="#">More Info</a></h4> 
      </div> 
      <div class="clear"></div> 
     </div>       
    </div> 
    <div class="grid_1_of_4 images_1_of_4"> 
     <h4><a href="#">item 08</a></h4> 
     <div class="price-details"> 
      <div class="price-number"> 
       <p><span class="price">$ price 08 </span></p> 
      </div> 
      <div class="add-cart">        
       <h4><a href="#">More Info</a></h4> 
      </div> 
      <div class="clear"></div> 
     </div>       
    </div> 
</div> 

我認爲它應該使用嵌套循環,但我不知道如何在laravel中編寫它。

任何人有任何想法應該如何?

+0

您的'print_r($ product');退出;'返回之前? – aldrin27

回答

4

http://laravel.com/api/5.1/Illuminate/Database/Eloquent/Collection.html#method_chunk

我喜歡塊()函數。比array_chunk更具可讀性和可重用性

@foreach ($product->chunk(4) as $chunkedProducts) 
<div class="section group"> 
@foreach ($chunkedProducts as $item) 
<div class="grid_1_of_4 images_1_of_4"> 
    <h4><a href="#">{!! $item->name !!}</a></h4> 
    <div class="price-details"> 
     <div class="price-number"> 
      <p><span class="price">$ {!! $item->price !!} </span></p> 
     </div> 
     <div class="add-cart">        
      <h4><a href="#">More Info</a></h4> 
     </div> 
     <div class="clear"></div> 
    </div>       
</div>   
@endforeach 
</div> 
@endforeach 
+0

絕對有效,簡單易讀。非常感謝。 – wahyueka31

0

This question正在解決一個非常類似的問題,看看。

報價:

你可以在$產品 - >使用array_chunk所有()將其在4組拆分,並進行嵌套@foreach。

在這種情況下,它可能看起來像這樣(沒有測試!):

@foreach (array_chunk($product->all(), 4) as $productSection) 
    <div class="section group"> 
     @foreach ($productSection as $item) 
      <div class="grid_1_of_4 images_1_of_4"> 
       <h4><a href="#">{!! $item->name !!}</a></h4> 
       <div class="price-details"> 
        <div class="price-number"> 
         <p><span class="price">$ {!! $item->price !!} </span></p> 
        </div> 
        <div class="add-cart">        
         <h4><a href="#">More Info</a></h4> 
        </div> 
        <div class="clear"></div> 
       </div>       
      </div> 
     @endforeach 
    </div> 
@endforeach 
+0

** @ pustur **,嗨,你的方法運行良好,非常感謝給我一個溶劑。你的方法與** @LêTrầnTiếnTrung **方法給出相同的結果,但** @LêTrầnTiếnTrung **更加簡單易讀,非常感謝。 – wahyueka31