Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add withExists method to QueriesRelationships #37302

Merged
merged 1 commit into from May 14, 2021
Merged

[8.x] Add withExists method to QueriesRelationships #37302

merged 1 commit into from May 14, 2021

Conversation

bastien-phi
Copy link
Contributor

This PR is another implementation of #37295 which adds less code to the framework.

I won't be sad if it is not merged but I wanted to provide another way of implementing the same behaviour.

For more information about the motivations of this feature, I add here the original description.

This PR adds withExists method to QueriesRelationships.

If we want to list our Users with an extra columns is_author (based on the fact that the user wrote a Post), at the moment we can only do something like

$users = User::withCount('posts')->get();
//...
$isAuthor = $user->posts_count > 0;

It works but it is not very efficient as the database needs to count every posts whereas we only need to know if there is at least one of them.

With this PR, we could simply do an existence check with sql exists wich is more efficient

$users = User::withExists('posts')->get();
//...
$isAuthor = $user->posts_exists;

The column name can also be aliased

$users = User::withExists('posts as is_author')->get();
//...
$isAuthor = $user->is_author;

Relations can be filtered and multiple relation existences can be fetched at the same time :

$users = User::withExists([
        'posts as is_author',
        'posts as is_tech_author' => function ($query) {
            return $query->where('category', 'tech');
        },
        'comments',
    ])->get();
//...
$user->is_author;
$user->is_tech_author;
$user->comments_exists;

In a future PR we could add loadExists for both Model and Illuminate\Database\Eloquent\Collection

@GrahamCampbell GrahamCampbell changed the title Add withExists method to QueriesRelationships [8.x] Add withExists method to QueriesRelationships May 7, 2021
@igorgaming
Copy link

I think that it was rejected not only because of the large code, most likely there are other reasons for this.
In fact, I've been thinking about this functionality for a long time.
Even if it is closed, I think I will add it to my project via macro(), but it would be great if it was part of the framework

@taylorotwell taylorotwell merged commit 11387ec into laravel:8.x May 14, 2021
@bastien-phi bastien-phi deleted the add_withExists_v2 branch May 17, 2021 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants