
Use withCount() to get total number of records with relationship
Use withCount() to get total number of records with relationship
In this session, we will try our hand at solving the "Use withCount() to get total number of records with relationship".
-
Get number of records with relationships in Laravel Eloquent using withCount() method
//app\Http\Controllers\<YourController>.php public function index() { $posts = Post::withCount(['comments'])->get(); return view('posts', compact('posts')); } //resources\view\posts.blade.php <table> @foreach ($posts as $post) <tr> <td>Post <b> {{ $post->title }} </b> has <b> {{ $post->comments_count }} </b> comments </td> </tr> @endforeach </table> //app\Models\Post.php public function comments() { return $this->hasMany('App\Models\Comment'); }
You can get the relationship records count without loading data using withCount() method. You have to first define the comments method with hasMany relationship in your post model. If you don’t know how to create hasMany relation you can find it here laravel hasMany method. After that you can call withCount() method on comments method which returns you the number of comments associated with this particular post without actually loading them (data from relationship table).
This code snippet has been tested and it's working in laravel 8. First you have to define a route in web file, create a hasMany relationship method (comments) in post model, create a method in controller file and return data to view and after that dispaly data in view file.
Post Quibusdam ipsam est nihil explicabo. has 3 comments
Post Inventore cupiditate dolores officiis enim accusantium. has 4 comments
Post Ea delectus et incidunt. has 1 comments
Post Excepturi ducimus quo sapiente nesciunt amet. has 1 comments
Post Asperiores maxime maiores sed. has 1 comments
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 display 1 day ago in comments in laravel view
- Laravel 9 route group with controller
- How to pass data to partial view file in laravel
- How to get count of all records created at yesterday
- Define variable and use in Laravel controller method
- How to generate .env file for laravel?
- Conditional where clause in Laravel
- Convert input array to comma-separated string in laravel controller
- Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0"
- Laravel 7 login error message not showing
- Store logged in user details in session and display in view in laravel
- How to check find method executed successfully in laravel
- How to add foreign key in laravel using migration
- How to pass external link in laravel blade to anchor tag
- How to get random string in Laravel
- Get last year created records in Laravel
- How to increment column value of table in Laravel
- Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()
- Insert data with form validation using ajax in laravel
- How to customize pagination view in laravel
- How to use or operator in laravel
- How to run a specific seeder class in laravel
- How to get route name on visit URL in laravel
- Wheredate in laravel not working
- Laravel clone model