The whereRelation (A better version of whereHas) method in Laravel 8.x

1 · Amit Merchant · Sept. 5, 2021, 1:01 p.m.
Checking the existence of relationships and fetching model records is quite easy in Laravel. The existing whereHas method The new whereRelation method for instance, let’s say, you want to retrieve all books which at least have one review, you can do it like so. use App\Models\Book; $books = Book::has('reviews')->get(); The existing whereHas method Now, if you want to go deeper with this, for example, if you only want to retrieve the books which have more than 3 stars, you can use the whereHa...