
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
- How to check data inserted or deleted in pivot after toggle method
- External link not working in laravel blade
- How to get id of next record in laravel
- Add [name] to fillable property to allow mass assignment on [App\Models\Project]
- How to pass external link in laravel blade to anchor tag
- If condition in foreach loop in laravel
- Validation for multiple forms on same page in laravel
- Target class [App\Http\Controllers\Auth\Request] does not exist.
- How to show data by ID in laravel?
- How to validate URL with https using regex in laravel
- Create record with unique slug in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1055
- Database transactions in laravel
- How to send ID to another page in Laravel
- Laravel create multiple records in Pivot table
- The use statement with non-compound name 'DB' has no effect
- Laravel pagination links with query string
- Call to a member function pluck() on array
- Get id of last inserted record in laravel
- Call to a member function getRelationExistenceQuery() on array in Laravel
- How to get all route list
- Remove array keys and values if it does not exist in other array in Laravel
- How to get CSRF token in laravel controller
- Define variable and use in Laravel controller method
- How to display order by null last in laravel