Skip to content

Can't search on other fields then name #63

Closed

Description

@ndijkstra

If I have in my dependency a filter to search on name or email, I don't see any results in the spotlight if I search on the email address. I do see that the result is returned in the debug bar.

Schermafbeelding 2021-11-25 om 13 27 33
Schermafbeelding 2021-11-25 om 13 27 10

Activity

  1. PhiloNL commented on Nov 25, 2021

    @PhiloNL
    Contributor

    You can now add synonyms to your search dependencies as well. For example:

    <?php
    
    namespace App\Spotlight;
    
    use App\Models\User;
    use LivewireUI\Spotlight\Spotlight;
    use LivewireUI\Spotlight\SpotlightCommand;
    use LivewireUI\Spotlight\SpotlightCommandDependencies;
    use LivewireUI\Spotlight\SpotlightCommandDependency;
    use LivewireUI\Spotlight\SpotlightSearchResult;
    
    class ViewLicensee extends SpotlightCommand
    {
        protected string $name = 'View user';
    
        public function dependencies(): ?SpotlightCommandDependencies
        {
            return SpotlightCommandDependencies::collection()
                ->add(
                    SpotlightCommandDependency::make('user')
                    ->setPlaceholder('Which user do you want to view?')
                );
        }
    
        public function searchUser($query)
        {
            return User::query()
                ->where('name', 'like', "%$query%")
                ->orWhere('email', 'like', "%$query%")
                ->take(10)
                ->get()
                ->map(function(User $user) {
                    return new SpotlightSearchResult(
                        $licensee->id,
                        $licensee->name,
                        sprintf('View %s', $user->name),
                        [
                            $licensee->email
                        ]
                    );
                });
        }
    
        public function execute(Spotlight $spotlight, User $user)
        {
            //
        }
    }
  2. ndijkstra commented on Nov 25, 2021

    @ndijkstra
    Author

    Awesome! Thanks for your quick response and change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions