1@forelse ($users as $user)
2 <li>{{ $user->name }}</li>
3@empty
4 <p>No users</p>
5@endforelse
1@foreach ($users as $user)
2 <p>This is user {{ $user->id }}</p>
3@endforeach
4
5@forelse ($users as $user)
6 <li>{{ $user->name }}</li>
7@empty
8 <p>No users</p>
9@endforelse
1public function index()
2
3{
4
5 $products = Product::get();
6
7 return view('home',compact('products'));
8
9}
10
11<div class="card-header">
12
13 <h5>Laravel Check Array Empty in Blade </h5>
14
15</div>
16
17<div class="card-body">
18
19 @forelse ($products as $product)
20
21 <p class="bg-danger text-white p-1">product</p>
22
23 @empty
24
25 <p class="bg-danger text-white p-1">No product</p>
26
27 @endforelse
28
29</div>
1$loop->index Returns a 0-based current loop iteration; 0 would mean the first iteration
2$loop->iteration Returns a 1-based current loop iteration; 1 would mean the first iteration
3$loop->remaining Number of iterations remaining in the loop; if there are a total of 10 iterations and the current iteration is 3, it would return 7
4$loop->count Returns the total number of iterations or the total number of items in the array
5$loop->first Returns true if it is the first iteration or item in the loop else returns false.
6$loop->last Returns true if it is the last iteration or item in the loop else return false.
7$loop->depth Returns the depth or nesting level of the current loop; returns 2 if it is a loop within a loop and 3 if it is nested one level more
8$loop->parentIf this loop is nested within another @foreach loop, parent returns the parent’s loop