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

Call queryCallback in DatabaseEngine #591

Merged
merged 1 commit into from
Feb 18, 2022

Conversation

Alanaktion
Copy link
Contributor

@Alanaktion Alanaktion commented Feb 18, 2022

The ->query() callback function on searches is helpful for things like eager loading relations, and is typically triggered via getScoutModelsByIds in engine implementations. Since the Database engine works directly with the models and does not call that method, I've added an explicit call to queryCallback when it's defined.

In my limited testing, this allows things like the following to work consistently on several engines including MeiliSearch and Database without requiring any engine-specific end-user code changes:

Post::search('example query')
    ->query(function ($builder) {
        $builder->with('comments');
    })
    ->paginate(15);

It doesn't seem like that callback is documented so it may not be widely used, but since it is implemented consistently in the other official engines this seemed like a good contribution.

This is typically triggered via getScoutModelsByIds, but the Database engine works directly with the models and does not call that method.
@taylorotwell
Copy link
Member

I think the intention is for you to use this: https://laravel.com/docs/9.x/scout#customizing-engine-searches ... for the database engine it receives the query as its first argument.

@Alanaktion
Copy link
Contributor Author

That does require the user to write engine-specific search queries rather than have a generalized solution that works consistently with one implementation, but if that's what's preferred by the Laravel team that's fine I guess. I'll probably just not use Scout at all for the database engine if I have to write custom logic to perform simple searches across multiple engines either way.

@taylorotwell taylorotwell reopened this Feb 18, 2022
@taylorotwell taylorotwell merged commit d3a618f into laravel:9.x Feb 18, 2022
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.

2 participants