
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
- Get the post details if it has at least one comment in comments table
- How to create laravel project using composer
- Use withCount() to get total number of records with relationship
- Laravel 9 route group with controller
- Pass variable from blade to controller Laravel
- Class "App\Http\Controllers\Auth\Verified" not found
- Store logged in user details in session and display in view in laravel
- InRandomOrder() method with example in laravel
- How to get all route list
- How to return a column with different name in Laravel
- How to get last year records count with month wise in Laravel
- Cast Array to an Object in Controller and then pass to view in laravel
- Get laravel version
- How to check find method executed successfully in laravel
- If condition in Laravel 9
- Extract only time from datetime in laravel
- Ignore Records where a field has NULL value in Laravel
- Argument 1 passed to App\Http\Controllers\Auth\LoginController::authenticated() must be an instance of App\Http\Controllers\Auth\Request
- How to fetch single row data from database in laravel
- How to pass data to partial view file in laravel
- Convert multidimensional array to single array in Laravel
- How to remove package from laravel
- How to get route method name in Laravel
- How to get route name on visit URL in laravel
- Print last executed query in laravel