1

我在我的网站上使用spatie/laravel-searchable。它在此功能中运行良好:

  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);
  }

但在某些方面(如:cu006),我有这个错误:

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)
4

2 回答 2

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);
}

请添加您的模型。

于 2020-06-16T05:34:23.327 回答
1

在您的模型中,当您创建getSearchResult函数时

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

如果您编写,则$this->title需要确保您的模型实际上包含该title字段,否则它会给您该错误。

于 2020-05-10T18:52:03.483 回答