1

im using spatie/laravel-searchable for my website. it works very well in this function:

  public function index(Request $request)
  {
    $results = (new Search())
    ->registerModel(Product::class, 'name', 'price','barcode')
    ->registerModel(Category::class, 'name')
    ->registerModel(Catalog::class, 'name')
    ->registerModel(Color::class, 'fatitle','entitle')
    ->search($request->input('query'));
    return response()->json($results);
  }

but in some words(like:cu006), i have this error:

Argument 2 passed to Spatie\Searchable\SearchResult::__construct() must be of the type string, null given
vendor/spatie/laravel-searchable/src/SearchResult.php:19
public function __construct(Searchable $searchable, string $title, ?string $url = null)
apokryfos
  • 38,771
  • 9
  • 70
  • 114

2 Answers2

1

In your model, when you create the getSearchResult function

 public function getSearchResult(): SearchResult
 {
     return new \Spatie\Searchable\SearchResult(
        $this,
        $this->title
     );
 }

If you write $this->title you need to make sure that your model actually contains the title field, if it doesn't it'll give you that error.

Alexandre Elshobokshy
  • 10,720
  • 6
  • 27
  • 57
  • That saved me!! thanks alot. do you know if there is any way show other fields under title? like description to create a proper search result page – stoneshaq May 26 '20 at 23:46
  • @stoneshak there might be but I'm not sure, I wrote this answer when I came across the problem myself and started debugging, you'd need to try things out and see, or create another question – Alexandre Elshobokshy May 27 '20 at 07:51
1
public function getSearchResult(): SearchResult
{
    $companySlug = currentCompanySlug();

    $url = url('/'.$companySlug.'/'.config('global-search- url.'.class_basename($this)));
    $null = null;
    return new SearchResult($this, $this->field_name ?:$null, $url);
}

please add in your model.

pankaj
  • 31
  • 1
  • 2