wire-elements / spotlight

Livewire component that brings Spotlight/Alfred-like functionality to your Laravel application.
MIT License
911 stars 71 forks source link

Can't search on other fields then name #63

Closed ndijkstra closed 2 years ago

ndijkstra commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

Awesome! Thanks for your quick response and change