
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
- Update existing pivot table data in laravel
- How to upload image in laravel 8
- Get today records in Laravel
- In order to use the Auth::routes() method, please install the laravel/ui package
- Target class [App\Http\Controllers\Auth\Request] does not exist.
- Get count of filter data, while return a small set of records
- How to display validation error in laravel
- Database transactions in laravel
- Cast Array to an Object in Controller and then pass to view in laravel
- Array to string conversion laravel Controller
- Laravel 9 route group with controller
- Route [password.request] not defined
- Laravel upload file with original file name
- Class 'App\Rules\Hash' not found in Laravel
- SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table
- Permanently delete a record in laravel
- Include External CSS and JS file in Laravel
- How to get date from created_at field in laravel
- Class 'App\Http\Controllers\User' not found
- Sample .htaccess file and index.php file under public directory in laravel
- Illuminate\Database\QueryException could not find driver
- How to create projects method with belongstomany relationship in user model
- Laravel hasmany select not working
- Get products with number of orders in Laravel
- Show old value while editing the form in Laravel