Split an Eloquent Collection by half in Laravel
Split an Eloquent Collection by half in Laravel
We will use programming in this lesson to attempt to solve the "Split an Eloquent Collection by half in Laravel".
Sometimes you have to split an Eloquent collection by half in Laravel view. You can use chunk method with ceil to divide records in two equal portion and display in view file using foreach() loop.-
Divide eloquent collection in two equal portion and display in 2 columns of a row in Laravel view
//routes\web.php Route::get('/get-users', function(){ $allUsers = App\Models\User::all(); return view('home', compact('allUsers')); }); //resources\views\home.blade.php <div class="container"> <div class="row"> @foreach($allUsers->chunk(ceil($allUsers->count()/2)) as $users) <div class="col-md-6"> @foreach($users as $user) <p> {{ $user->name }} </p> @endforeach </div> @endforeach </div> </div>
This code snippet will help you to display the records in two columns of a row with equal numbers of items. It will always be 1 element larger in the event that the array contains an odd number of elements.
You have to add the bootstrap CDN in your view file to display these two columns side by side. You can also display records in three to four equal columns in by changing value form 2 to 3 or 4 as per your requirment in ceil() method.
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
- Route [password.request] not defined
- Link storage folder in laravel 8
- SQLSTATE[42000]: Syntax error or access violation: 1055
- Update email with unique validation in laravel
- Laravel 7 login error message not showing
- How to get list of all views file in laravel
- Generate random string lowercase in Laravel
- Permission denied error while creating storage link in Laravel
- Include External CSS and JS file in Laravel
- Laravel save object to database
- Laravel route parameter
- Best Practices for Error Handling in Production Server Code (example code)
- Post table seeder laravel 10
- 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 add active class to menu item in laravel
- How to pass query string with pagination in laravel
- How to print form data in laravel
- Run artisan command to generate key in laravel
- Array to string conversion laravel Controller
- How to upload multiple images after preview in laravel using cropper js
- How to validate URL with https using regex in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
- Display option of select as selected with blade directive Laravel
- Get last record from table in laravel
- How to use bootstrap pagination in laravel 8