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

Using Scout, relationship search on HasMany field returns results from all resources, not scoped to certain resource #6622

Closed
ekateiva opened this issue Dec 19, 2024 · 4 comments
Labels
bug Verified bug by the Nova team fix incoming A fix is in review
Milestone

Comments

@ekateiva
Copy link

ekateiva commented Dec 19, 2024

  • Laravel Version: 11.30.0
  • Nova Version: v4.35.4 (Silver Surfer)
  • PHP Version: 8.3.9
  • Database Driver & Version: MySQL 8 + Scout (Algolia)
  • Operating System and Version: MacOs
  • Browser type and version: Chrome (130.0.6723.92)
  • Reproduction Repository: https://github.com/ekateiva/laravel_nova_issue_6622/

Description:

When using a HasMany relationship between resources (e.g., Supplier and Product where a Supplier has many Products), searching through the related resource from the parent resource's page does not properly scope the results.
E.g. In certain supplier page (/nova/resources/suppliers/60) I search for Product with query test, and the results come from all supplier, not the one with ID 60. Product resource has Scout enabled.

Detailed steps to reproduce the issue on a fresh Nova installation:

  1. Define a Supplier resource with a HasMany relationship to Product:
public function fields(Request $request)
{
    return [
        HasMany::make('Products'),
    ];
}
  1. Use Laravel Scout for searching in the Product resource.
public static function usesScout(): bool
{
    return true;
}
  1. Navigate to a specific Supplier page in Laravel Nova.
  2. Use the search functionality in the Products relationship field.
  3. Observe that the search results include Products from all Suppliers, not just the current Supplier.

Workaround

As a workaround, this code can be used in Product resource:

public static function scoutQuery(NovaRequest $request, $query): \Laravel\Scout\Builder
{
    return $query
        ->when(
            $request->viaResource() && $request->viaResourceId,
            function (\Laravel\Scout\Builder $query) use ($request) {
                $algoliaIndexedField = match ($request->viaResource) {
                    Supplier::uriKey() => 'supplier_id',
                    default => null,
                };

                if (! $algoliaIndexedField) {
                    \\ Log...

                    return $query;
                }

                return $query->where($algoliaIndexedField, $request->viaResourceId);
            },
        );
}

In Laravel Nova documentation you can find this:

For this reason, Nova integrates seamlessly with Laravel Scout. When the Laravel\Scout\Searchable trait is attached to a model associated with a Nova resource, Nova will automatically begin using Scout when performing searches against that resource. There is no other configuration required.

So it's not totally true, if the search does not scope the results via relationships :)

@ekateiva ekateiva changed the title When using Scout, relationship search on HasMany field returns results from all resources, not scoped to certain resource Using Scout, relationship search on HasMany field returns results from all resources, not scoped to certain resource Dec 19, 2024
@crynobone
Copy link
Member

As Scout doesn't map relationship and doesn't quarantine supplier_id would always exist we filters by relationship using Eloquent and not Scout.

Please provide the dataset used so we can verify if the issue exists and we need to tweak the queries (if possible)

@crynobone crynobone added the needs more info More information is required label Dec 19, 2024
@sanderbaas
Copy link

We also ran into this problem, see #6259

@ekateiva
Copy link
Author

ekateiva commented Dec 20, 2024

Please provide the dataset used so we can verify if the issue exists and we need to tweak the queries (if possible)

@crynobone, I prepared a simplified demo repo with Product and Supplier models/resources. The repo has DB seeders: https://github.com/ekateiva/laravel_nova_issue_6622

It should be enough to track the issue.


An example:

Products having "premium" in name:
Screenshot 2024-12-20 at 17 07 39

Single supplier products, that has some "premium" products:
Screenshot 2024-12-20 at 17 07 47

Issue, when the search is used, the products are not scoped
Screenshot 2024-12-20 at 17 07 57

When Scout is off, everything is OK:
Screenshot 2024-12-20 at 17 08 12

@crynobone crynobone added the pending Issues that are pending triage label Dec 22, 2024
@crynobone crynobone added bug Verified bug by the Nova team and removed needs more info More information is required pending Issues that are pending triage labels Dec 30, 2024
@crynobone crynobone added this to the 5.1.x milestone Dec 30, 2024
@crynobone crynobone added the fix incoming A fix is in review label Dec 31, 2024
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 14, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Verified bug by the Nova team fix incoming A fix is in review
Projects
None yet
Development

No branches or pull requests

3 participants