
Display data in table using foreach in Laravel
Display data in table using foreach in Laravel
Good day, guys. In this post, we’ll look at how to solve the "Display data in table using foreach in Laravel" programming puzzle.
There are many ways to display data in a table using the foreach method in Laravel. We have to use an appropriate way to display data into a table so that it does not throw any exception while data goes through the foreach loop. To avoid these exceptions we have to first check if their records exist in it or not and after that, we can display data in it.-
Display data using count method on object with foreach in table
--PATH resources\views\<yourfile>.blade.php@if($posts->count()) @foreach($posts as $post) <tr> <td> {{ $post->title }} </td> <td> {{ $post->body }} </td> </tr> @endforeach @else <tr> <td colspan="2"> No record found </td> </tr> @endif
The count() method first checks if the object has any records or not, after that, it displays data in the table using the foreach method if the object has records in it. If the object does not have any records then it executes the else statement and displays no records found.
-
Display data in table using forelse loop Laravel
--PATH resources\views\<yourfile>.blade.php@forelse($posts as $post) <tr> <td> {{ $post->title }} </td> <td> {{ $post->body }} </td> <td> {{ $post->created_at }} </td> </tr> @empty <tr> <td colspan="3"> No record found </td> </tr> @endforelse
This code snippet is the shorter way to display data in the table using forelse loop in Laravel which displays data if any records exist in object else it displays no record found.
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
- How to get route name on visit URL in laravel
- Submit form without CSRF token in Laravel
- Link storage folder in laravel 8
- Laravel upload file with original file name
- How to get file extension from input type file in laravel
- How to start websocket server in laravel
- How to send email in laravel
- Include External CSS and JS file in Laravel
- How to create belongstomany relation using custom name on custom pivot table
- How to use or operator in laravel
- Generate random string lowercase in Laravel
- How to add active class to menu item in laravel
- Multiple Level eager loading in Laravel
- Add class to body in laravel view
- How to display validation error in laravel
- How to disable timestamps in laravel
- Cast Array to an Object in Controller and then pass to view in laravel
- Laravel 7 login error message not showing
- Database transactions in laravel
- Method Illuminate\Events\Dispatcher::fire does not exist
- Use withCount() to Calculate Child Relationship Records
- Pass variable from blade to controller Laravel
- How to authenticate admin users in Laravel ?
- Redirect to another view from controller in laravel
- InRandomOrder() method with example in laravel