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

Amit Merchant · September 5, 2021 ·

Checking the existence of relationships and fetching model records is quite easy in Laravel.

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 whereHas method like so.

use App\Models\Book;
use Illuminate\Database\Eloquent\Builder;

$books = Book::whereHas('reviews', function (Builder $query) {
    $query->where('stars', '>', 3);
})->get();

This is all nice but in the recent PR, an alternate method is added which does the same but with better developer experience.

The new whereRelation method

As I mentioned earlier, this PR for Laravel 8.x introduced a method called whereRelation which is essentially the whereHas method that offers better readability and less code complexity by omitting the closure altogether.

Here’s how the previous example can be written using whereRelation.

use App\Models\Book;

$books = Book::whereRelation('reviews', 'stars', '>=', 3)->get();

As you can tell, this method makes the query more readable without the need of passing a closure to it.

The PR also adds few more methods such as the orWhereRelation(), whereMorphRelation() and orWhereMorphRelation() for morph relations that works works the similar way as the whereRelation() method.

👋 Hi there! I'm Amit. I write articles about all things web development. If you like what I write and want me to continue doing the same, I would like you buy me some coffees. I'd highly appreciate that. Cheers!

Comments?