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

Can't search on other fields then name #63

Closed
ndijkstra opened this issue Nov 25, 2021 · 2 comments · Fixed by #64
Closed

Can't search on other fields then name #63

ndijkstra opened this issue Nov 25, 2021 · 2 comments · Fixed by #64

Comments

@ndijkstra
Copy link

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

@PhiloNL
Copy link
Contributor

PhiloNL commented Nov 25, 2021

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)
    {
        //
    }
}

@ndijkstra
Copy link
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
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants