๐ŸŽ‰๐ŸŽ‰ Larasense is officially launched ๐ŸŽ‰๐ŸŽ‰
- Your Hub for Laravel News, Trends & Updates

New Query Builder Methods Added in Laravel 11.43

laravel
release
query
Nabil Hassen
Nabil Hassen
Feb 19, 2025
New Query Builder Methods Added in Laravel 11.43
Last updated on Feb 19, 2025
Table of contents:

Laravel Adds withWhereRelation and findSole Methods to Query Builder

Two new methods have been introduced to Laravel's query builder: withWhereRelation and findSole. These improvements make querying relationships and retrieving single records more convenient and expressive. Let's dive into how they work.

withWhereRelation: Constraining Eager-Loaded Relationships

The new withWhereRelation method, contributed by @SomaiyaUtsav, allows developers to apply constraints to eager-loaded relationships directly. Previously, filtering related models required separate where constraints after eager loading. This new method simplifies that process.

Example Usage:

$users = User::query()
->withWhereRelation('posts', 'is_published', true)
->get();

This query retrieves users who have at least one published post, reducing the need for extra filtering operations.

findSole: Ensuring a Unique Record Match

The findSole method, introduced by @zepfietje, is a more restrictive version of find. It ensures that exactly one record matches the given primary key. If no record is found, or if multiple records exist, an exception is thrown.

Example Usage:

$user = User::query()->findSole($id);

Conclusion

These new methods enhance Laravel's query builder by making common tasks more intuitive. withWhereRelation provides a cleaner way to filter related models during eager loading, while findSole helps enforce data integrity by ensuring a single result. Both additions contribute to Laravelโ€™s expressive and developer-friendly approach to database querying.

For more details, check out the official pull requests:

Nabil Hassen
Nabil Hassen
Full Stack Web Developer

Stay Updated.

I'll you email you as soon as new, fresh content is published.

Thanks for subscribing to my blog.

Latest Posts