Shorter syntax for whereHas with call back function in laravel
Shorter syntax for whereHas with call back function in laravel
With this article, we’ll look at some examples of how to address the "Shorter syntax for whereHas with call back function in laravel" problem.
Shorter syntax for whereHas with call back function in laravel-
Shorter syntax for whereHas with call back function in laravel
$posts = Post::whereHas('comments', function($query){ $query->where('post_id', 1); })->get(); //Shorter syntax $posts = Post::whereRelation('comments', 'post_id', 1)->get();
This code snippet will return the post details if there are comments regarding this particular post (post_id is 1 in comments table) exists in the comments table else it will return a blank array in laravel.
It will also helps you to get the post record which has at least one comment 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
- Insert data with form validation using ajax in laravel
- Page loader in laravel
- How to run a specific seeder class in laravel
- How to customize pagination view in laravel
- There are no commands defined in the "route:" namespace
- Ignore Records where a field has NULL value in Laravel
- Get only 10 records from table in laravel
- Laravel 9 pagination with search filter
- Send id with route Laravel
- How to get route method name in Laravel
- Get previous date data in laravel
- Class 'App\Http\Controllers\User' not found
- Laravel file size validation not working
- Route prefix with auth middleware in laravel
- How to get list of all views file in laravel
- Use withCount() to get total number of records with relationship
- Delete all related comments on deleting a post in Laravel
- How to fill a column automatically while creating records in Laravel
- How to remove package from laravel
- How to check duplicate entry in laravel
- Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()
- Json encode method in laravel
- How to Run CRON Job on LIVE SERVER on Cpanel in Laravel Project
- Call to undefined method Illuminate\Support\Facades\Request::all()
- How to add unique records in pivot columns of Laravel pivot table