wire-elements / spotlight

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

Spotlight is not reaching to 'execute' method. (404) #41

Closed dhruva81 closed 3 years ago

dhruva81 commented 3 years ago

Hi

I have read complete documentation and also I read tutorial blog post.

I tried using all steps mentioned in your documentation but this package is not working on final 'execute' method step in my application :(

I am able to open spotlight using shortcut command 'CTRL + K' and able to search users.

But when I select any user and press enter, I am receiving 404.

The method 'execute' never called and in console I am receiving 404 error for post request and Uncaught (in promise) undefined for livewire.js

      protected string $name = 'View Student Profile';
      protected string $description = 'Redirect to student profile page';

      public function dependencies(): ?SpotlightCommandDependencies
      {
            return SpotlightCommandDependencies::collection()
                  ->add( SpotlightCommandDependency::make('student') ->setPlaceholder('Enter student name or email.') );
      }

      public function searchStudent($query)
      {
            return User::where('name', 'like', "%$query%")
                  ->get()
                  ->map(function(User $student) {
                        return new SpotlightSearchResult(
                              $student->id,
                              $student->name,
                              sprintf('Go to profile page for %s', $student->name)
                        );
                  });
      }

      public function execute(Spotlight $spotlight, User $student)
      {
              $spotlight->redirectRoute('students.show', $student);
      }

      public function shouldBeShown(): bool
      {
            return true;
      }

I have also registered command in AppServiceProvider.

My application is built on Laravel 8.12 Php 8 Alpinejs 3.2.2 Livewire 2.5.5

Kindly guide me. Thank you !

PhiloNL commented 3 years ago

Could you post the XHR request details (route, request headers, response headers, etc) from your developer tools?

Thanks!

jplhomer commented 2 years ago

FYI, for future people coming across this:

It happened to me when my model had a custom getRouteKeyName() override (in my case, slug). Since Livewire tries to assemble arguments based on route binding, and I was returning id from the search results, it was throwing a 404 not found.