
First and last item of the array using foreach iteration in laravel blade
First and last item of the array using foreach iteration in laravel blade
In this session, we will try our hand at solving the "First and last item of the array using foreach iteration in laravel blade".
-
First/last item of the array in laravel
--PATH resources/views/<yourfile>.blade.php@foreach ($users as $user) @if ($loop->first) This is the first iteration. @endif @if ($loop->last) This is the last iteration. @endif <p>This is user {{ $user->id }}</p> @endforeach
This code snippet helps you to get the first and last item of the array through the loop iteration. This array variable holds useful information like current loop index and whether this is the first or last iteration through the loop. This is a very useful function to get the first and last elements of an array in laravel using foreach in laravel blade file.
This is useful to add css classes to the first and last element in the array through loop iteration and you can manipulate first and last elements of the array as per your requirement.
-
Add class to first item and the last item of array using foreach in laravel blade file
--PATH resources/views/<yourfile>.blade.php<ul class="list-unstyled"> @foreach ($posts as $post) <li @if($loop->first) class="first-item" @endif> {{ $post->title }} </li> @endforeach </ul> <ul class="list-unstyled"> @foreach ($posts as $post) <li @if($loop->last) class="last-item" @endif> {{ $post->title }} </li> @endforeach </ul>
It helps you to get the first and last item of the array through the loop iteration, using if condition you can add class to first item and the last item of array using foreach method in laravel blade file. If the current item is the first item of array then it appends the first-item class to list item and in the other case if the current item of loop is the last item then it appends the last-item class to the list item.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Laravel insert query not working
- How to start websocket server in laravel
- How to make Copy or Duplicate table row in laravel
- Eager loading dynamically in laravel
- If condition in Laravel 9
- Call to undefined method Illuminate\Support\Facades\Request::all()
- How to create new user without form submission in laravel
- Retain selected value of select box in Laravel
- How to customize or Change validation error messages
- Show old value while editing the form in Laravel
- Update last created record in Laravel
- Call to a member function getRelationExistenceQuery() on array in Laravel
- Argument 1 passed to App\Http\Controllers\Auth\LoginController::authenticated() must be an instance of App\Http\Controllers\Auth\Request
- Class App\Http\Controllers\Admin\UserController Does Not Exist
- How to implement toggleLike() method in Overtrue\LaravelLike laravel package
- Update record after find method in lavavel
- Insert values in pivot table dynamically in laravel
- Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table (SQL: alter table `users` add constraint `users_role_id_foreign` foreign key (`role_id`) references `roles` (`id`))
- How to create laravel project using composer
- How to remove package from laravel
- How to check records exist in loaded relationship in Laravel blade view
- Add a subselect based on relationship using withAggregate method
- Always load the relationship data with eager loading in Laravel
- Laravel get single row by id
- The Pusher library requires the PHP cURL module. Please ensure it is installed