wire-elements / spotlight

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

Issue with resolution of models with two words #42

Closed Braunson closed 2 years ago

Braunson commented 2 years ago

In this case, this is my example spotlight command:

<?php

namespace App\Spotlight;

use App\Models\CrashCourse;
use LivewireUI\Spotlight\Spotlight;
use LivewireUI\Spotlight\SpotlightCommand;
use LivewireUI\Spotlight\SpotlightCommandDependencies;
use LivewireUI\Spotlight\SpotlightCommandDependency;
use LivewireUI\Spotlight\SpotlightSearchResult;

class FindCrashCourse extends SpotlightCommand
{
    protected string $name = 'Find crash course';
    protected string $description = 'Find a crash course';

    /**
     * Defining dependencies is optional. If you don't have any dependencies you can remove this method.
     * Dependencies are asked from your user in the order you add the dependencies.
     */
    public function dependencies(): ?SpotlightCommandDependencies
    {
        return SpotlightCommandDependencies::collection()
            ->add(
                SpotlightCommandDependency::make('crash course')
                ->setPlaceholder('What is the title or ID of the crash course you are looking for')
            );
    }

    /**
     * Spotlight will resolve dependencies by calling the search method followed by your dependency name.
     * The method will receive the search query as the parameter.
     */
    public function searchCrashCourse($query)
    {
        $input = "%$query%";

        return CrashCourse::query()
            ->orWhere('title', 'LIKE', $input)
            ->take(10)
            ->orderByDesc('created_at')
            ->get()
            ->map(function(CrashCourse $crashCourse) {
                return new SpotlightSearchResult(
                    $crashCourse->id,
                    $crashCourse->title,
                    'View crash course'
                );
            });
    }

    /**
     * When all dependencies have been resolved the execute method is called.
     * You can type-hint all resolved dependency you defined earlier.
     */
    public function execute(Spotlight $spotlight, CrashCourse $crashCourse)
    {
        // The issue here is it's returning null for $crashCourse
        dd($crashCourse);

        $spotlight->redirectRoute('admin.crash-course.details', $crashCourse);
    }
}

When diving deeper into the logic that calls the execute method, it's returning crash course with a space. I've tried multiple forms $crash_course, $CrashCourse, $crash_Course` and nothing seems to work here.

If I dump out the dependencies from Livewire's ImplicitlyBoundMethodClass I get this.

array:4 [▼
  0 => "d6651b5f3cdb0b88d94529c6355233eb"
  1 => "crash course"
  2 => "test"
  3 => array:1 [▼
    "crash course" => 1
  ]
]
PhiloNL commented 2 years ago

I think everything should work if you change the variable names to one word:

SpotlightCommandDependency::make('crash course')

// into

SpotlightCommandDependency::make('crashCourse')
Braunson commented 2 years ago

Thanks @PhiloNL, not sure why I realize that. Cheers!