
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
- Get domain name in laravel
- Insert current date time in a column using Laravel
- How to display pivot table column value in laravel
- Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
- Laravel migration add foreign key to existing table
- Array to string conversion laravel blade
- Get count of filter data, while return a small set of records
- Use withCount() to get total number of records with relationship
- Laravel get count with where condition
- Laravel save object to database
- Delete file from amazon s3 bucket using Laravel
- Method chaining in Laravel
- How to customize pagination view in laravel
- Convert input array to comma-separated string in laravel controller
- Method Illuminate\Http\Request::validated does not exist
- How to validate URL with https using regex in laravel
- How to call controller function from view in Laravel
- Array to string conversion laravel Controller
- Comment .env file in laravel
- How to get all route list
- The use statement with non-compound name 'DB' has no effect
- How to get random string in Laravel
- Calculate age from date of birth in Laravel
- Store logged in user details in session and display in view in laravel
- How to automatically update the timestamp of parent model in Laravel