
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
- Call to undefined relationship [user] on model [App\Models\Post]
- How to get records in random order in laravel
- OrderBy on Eloquent relationships method in Laravel
- Get products with number of orders in Laravel
- How to get last year records count with month wise in Laravel
- First and last item of the array using foreach iteration in laravel blade
- How to get specific columns using Laravel eloquent methods
- How to get selected categories on edit record with Select2
- Class 'App\Rules\Hash' not found in Laravel
- Root composer.json requires php ^7.3 but your php version (8.0.0) does not satisfy that requirement
- Php artisan make model, factory, migration and controller in single command
- How to Access Array in blade laravel
- If no route matched route::fallback in laravel
- On delete set foreign id column value null using migration in laravel 8
- Redirect from www to non www in laravel using htaccess
- How to get last record from object collection in laravel
- How to get list of all views file in laravel
- Call to undefined method App\Models\User::follow()
- Rename Pivot Table in Laravel
- How to print form data in laravel
- How to Run CRON Job on LIVE SERVER on Cpanel in Laravel Project
- Target class [App\Http\Controllers\Auth\Request] does not exist.
- Credit card validation in laravel
- Get comma separated email from input array
- How to get all route list