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
- Laravel 11 step by step instructions to upload file in storage directory and display in blade file
- Class App\Http\Controllers\Admin\UserController Does Not Exist
- Get only 10 records from table in laravel
- Target class [HomeController] does not exist
- Get ids in array from users table
- How to check query string exists or not in laravel blade
- Get count of filter data, while return a small set of records
- Delete file from amazon s3 bucket using Laravel
- How to get only time from created_at in laravel
- Class "App\Http\Controllers\Auth\Verified" not found
- How to display validation error in laravel
- Remove public from url in laravel project
- Get products with number of orders in Laravel
- Cast Array to an Object in Controller and then pass to view in laravel
- Laravel recursive function in controller
- How to display HTML tags In Laravel blade
- Use withCount() to Calculate Child Relationship Records
- Delete records with relationship in laravel
- Array to string conversion laravel blade
- Where to use whereNotNull eloquent in laravel
- Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()
- How to get last record from object collection in laravel
- How to add foreign key in laravel using migration
- Retain selected value of select box in Laravel
- How to upload files to amazon s3 bucket using Laravel