في هذا المقال سوف نتطرق لبعض الخصائص التي يمكن إستخدامها مع foreach في ملفات blade، مثل كيف نعرف عدد الصفوف، كيف يمكن إيقاف loop عند شرط معين، وكيف يمكن إستبدال الكود التالي بسطر واحد.

 @if($products->count())
     @foreach ($products as $item)
         <tr>
             <th>{{ $item->id }}</th>
             <td>{{ $item->title }}</td>
             <td>${{ number_format($item->price,2) }}</td>
             <td>{{ $item->cat->title }}</td>
         </tr>
     @endforeach
 @else
     <div class="alert alert-danger">No Products Added</div>
 @endif

حيث يعطي النتيجة التالية

كيف يمكن إعطاء السطر الأول لون مختلف

@if($products->count())
     @foreach ($products as $item)
         <tr @if($loop->first) class="bg-info" @endif>
             <th>{{ $item->id }}</th>
             <td>{{ $item->title }}</td>
             <td>${{ number_format($item->price,2) }}</td>
             <td>{{ $item->cat->title }}</td>
         </tr>
     @endforeach
 @else
     <div class="alert alert-danger">No Products Added</div>
 @endif

 

نلاحظ بالكود أعلاه أننا قمنا بإستخدام $loop->first، لكن من إين جاء المتغير loop؟

جاء المتغير $loop لأنه بداخل أي foreach في Laravel blade يوجد هذا المتغير $loop مع مجمووعة من الخصائص مثل :

$loop->index = السطر/ العنصر حسب رقم  index

<tr @if($loop->index == 4) class="bg-info" @endif>

هنا يتم تطبيق الكلاس على العنصر الثالث لأن index يبدأ من صفر

  • $loop->iteration = يبدأ العد من ١ وليس من صفر مثل الـ index
  • $loop->remaining: العناصر المتبقية من loop
  • $loop->count : عدد العناصر.
  • $loop->even : العناصر الزوجية .
  •   : $loop->oddالعناصر الفردية.
  • $loop->first : العنصر الأول:
  • $loop->last : العنصر الأخير 

مثال على loop->iteration

@foreach ($products as $item)
     <tr @if($loop->iteration == 4) class="bg-info" @endif>
         <th>{{ $item->id }}</th>
         <td>{{ $item->title }}</td>
         <td>${{ number_format($item->price,2) }}</td>
         <td>{{ $item->cat->title }}</td>
     </tr>
 @endforeach

حيث سيتم هنا تطبيق الكلاس bg-info على السطر الرابع.

 

لإيقاف loop إذا تحقق شرط معين

مثلا إذا كان السعر أكبر من 15 أن يتم إيقاف loop

@foreach ($products as $item)
     <tr>
         <th>{{ $item->id }}</th>
         <td>{{ $item->title }}</td>
         <td>${{ number_format($item->price,2) }}</td>
         <td>{{ $item->cat->title }}</td>
     </tr>
     @if($item->price > 15) @break @endif
 @endforeach

 

كيف يمكن تبسيط الكود أعلاه

الخطوة الأولى بدل من إستخدام count ومن ثم   foreach يمكن إستخدام forelse مع empty

@forelse ($products as $item)
     <tr>
         <th>{{ $item->id }}</th>
         <td>{{ $item->title }}</td>
         <td>${{ number_format($item->price,2) }}</td>
         <td>{{ $item->cat->title }}</td>
     </tr>
 @empty
     <div class="alert alert-danger">No Products Added</div>
 @endforelse

 

الخطوه الثانيه هي فصل محتوى الجدول في ملف منفصل وعمل include له

@forelse ($products as $item)
     @include('admin.components.product',['item'=>$item])
 @empty
     @include('admin.components.no-product')
 @endforelse

الخطوة الثالثه: إختصار الكود أعلاه من خلال @each

@each('admin.components.product', $products, 'item','admin.components.no-product' )