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
- Laravel clone model
- Get today records in Laravel
- Update last created record in Laravel
- How to check if user has created any post or not in laravel
- Call to a member function pluck() on array
- Get posts belongs to a specific user in Laravel
- Symlink(): No such file or directory
- How to add unique records in pivot columns of Laravel pivot table
- Laravel URL validation not working
- How to send email in laravel
- How to display 1 day ago in comments in laravel view
- Convert multidimensional array to single array in Laravel
- Get all users except the followings users in overtrue laravel-follow
- How to add class to tr in table using foreach in laravel
- Get content from web URL in laravel
- How to call controller function from view in Laravel
- Send post data from controller to view
- Root composer.json requires php ^7.3 but your php version (8.0.0) does not satisfy that requirement
- How to delete record in Laravel with ajax
- SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'user_id'
- Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found
- How to call model in blade laravel
- Credit card validation in laravel
- JQuery each loop on json response after ajax in laravel
- How to pass query string to url in laravel