How to get all posts which contains comments in laravel
How to get all posts which contains comments in laravel
In this session, we’ll try our hand at solving the "How to get all posts which contains comments in laravel" puzzle by using the computer language.
Sometimes you have to get all the posts which contains comments in laravel. You have to use the whereHas() method with relationship name to get all the records which have records that exist in the relationship (child) table using foreign key relation.-
Get all posts which contains comments in laravel
$posts = Post::whereHas('comments')->get(); dd($posts);
This code snippet will return all the posts which has at least one comment exists in comments table.
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
- Retain selected value of select box in Laravel
- How to use bootstrap pagination in laravel 8
- Add class to body in laravel view
- Attempt to read property "avatar" on null in Laravel
- How to get records in random order in laravel
- Property [user] does not exist on this collection instance
- Laravel 5.4 save data to database
- In order to use the Auth::routes() method, please install the laravel/ui package
- Where to use whereNotNull eloquent in laravel
- Call to undefined method Illuminate\Support\Facades\Request::all()
- Class App\Http\Controllers\Admin\UserController Does Not Exist
- Send id with route Laravel
- The openssl extension is required for SSL/TLS protection but is not available
- How to create new user without form submission in laravel
- Target class [admin] does not exist.
- Get last year created records in Laravel
- Extra Filter Query on Relationships in Laravel
- Post model with title and body in laravel 8
- Route [password.request] not defined
- Class "App\Http\Controllers\Auth\Verified" not found
- Laravel get single row by id
- How to pass query string with pagination in laravel
- Get ids in array from users table
- How to create static page in Laravel
- How to add dynamic page title in Laravel view