【问题标题】:Laravel Spatie SearchableLaravel Spatie 可搜索
【发布时间】:2022-12-21 17:03:45
【问题描述】:

我正在尝试在我的项目中实现 spatie searchable,当我进行普通搜索时它工作正常。但是,如果我尝试进行一些过滤,它就不起作用,但我不知道。我在下面添加了我的代码:

我的控制器:

<?php

namespace App\Http\Livewire\SuperAdmin;

use Livewire\Component;
use Spatie\Searchable\Search;
use App\Models\Category;

class SuperAdminSearch extends Component
{
    public $query;
    public $searchResults = [];
    public $name = [];

    public function updated($property) {
        $this->name = $this->categoryName();
        if($property == 'query') {
            $searchterm = $this->query;
 
            $this->searchResults = (new Search())
                        ->registerModel(Category::class, 'name')
                        ->perform($searchterm);
        }

        if(empty($this->query)) {
            $this->searchResults = [];
        }
    }


    public function render()
    {
        return view('livewire.super-admin.super-admin-search');
    }
}

我的模型:

protected $fillable = ['name', 'category_type'];
public function getSearchResult(): SearchResult
{
    $url = route('super_admin_category_details', $this->id);

    return new SearchResult(
        $this,
        $this->name,
        $url
    );
}

现在我想做的是显示所有类别名称,其中 category_type 将是广告。就这样。但是我坚持了最后几天。

谢谢

【问题讨论】:

    标签: laravel laravel-livewire spatie-ssh laravel-searchable


    【解决方案1】:

    你必须使用SearchAspect。通过这种方式,您还可以搜索完全匹配项,甚至可以像使用查询构建器一样过滤您的查询。

        $searchResults = (new Search())
        ->registerModel(Category::class, function (ModelSearchAspect $modelSearchAspect) {
            $modelSearchAspect
            ->addSearchableAttribute('category_name')
            ->where('category_type', 'your_category_type_id');
        })->perform($searchterm);
    

    【讨论】:

      猜你喜欢
      • 2020-05-30
      • 1970-01-01
      • 2020-09-15
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 2018-12-12
      • 2021-02-16
      相关资源
      最近更新 更多