trilel
trilel

Reputation: 219

Adding a div row for a new row in Laravel

I am beginner in Laravel. I make my project in Laravel 8.

I have this code:

@foreach($productIngredients as $productIngredient)
@php
    if($selectedProductIngredients->contains('ingredient_id', $productIngredient->id) === true){
        $item = \App\Models\SelectedProductIngredient::where('product_id', $product->id)->where('ingredient_id', $productIngredient->id)->first();
        $weight = $item->weight;
    } else {
        $weight = null;
    }
@endphp

<div class="col-6">
    <div class="form-check py-2">
        <input id="productIngredientId-{{ $productIngredient->id }}"
               class="form-check-input enableInput" style="margin-top:10px"
               name="productIngredient[]" type="checkbox"
               value="{{ $productIngredient->id }}"
               @if($selectedProductIngredients->contains('ingredient_id', $productIngredient->id) === true) checked @endif>
        <label class="form-check-label" for="flexCheckChecked">
            {{ $productIngredient->name }} [{{ $productIngredient->short_name }}]
        </label>
        <input id="productIngredient-{{ $productIngredient->id }}" type="text"
               name="productIngredient-{{ $productIngredient->id }}" maxlength="10"
               class="form-control weight-input weightMask"
               style="width:100px;display: inline; margin-left:20px" placeholder=""
               value="{{ $weight }}">
    </div>
</div>
@endforeach

It's work fine.

I would like there to be 2 records in 1 row. So I would like to add at the beginning and close div in case of the last record

How can I do this?

Please help me.

Upvotes: 2

Views: 209

Answers (1)

W Kristianto
W Kristianto

Reputation: 9303

Do you know about the Laravel collection? It can help you.

You can use chunk method, it breaks the collection into multiple, smaller collections of a given size:

@foreach($productIngredients->chunk(2) as $chunk)
  <div class="row">
  @foreach($chunk as $productIngredients)
    <div class="col-6">
      ...
    </div>
  @endforeach
  </div>
@endforeach

Upvotes: 3

Related Questions