makidizajnerica/laravel-searcher

简单模型和多模型搜索。

v1.0.1 2022-03-24 16:29 UTC

This package is auto-updated.

Last update: 2024-09-24 21:54:54 UTC


README

简单模型和多模型搜索。

安装

composer makidizajnerica/laravel-searcher

模型准备

模型应实现 MakiDizajnerica\Searcher\Contracts\Searchable 接口,然后在其中定义 attributesForTags() 方法。此方法应返回将用作模型标签的属性值。之后添加 MakiDizajnerica\Searcher\Searchable 特性。

<?php

use Illuminate\Database\Eloquent\Model;
use MakiDizajnerica\Searcher\Searchable;
use MakiDizajnerica\Searcher\Contracts\Searchable as SearchableContract;

class Post extends Model implements SearchableContract
{
    use Searchable;

    /**
     * Get model attributes that will be used for tags.
     * 
     * @return array<int, string>
     */
    public function attributesForTags(): array
    {
        return $this->only(['title']);
    }
}

标签将在模型创建时自动创建,在模型更新时更新,并在模型删除时删除。

如果您想创建不带标签的模型,应使用 createWithoutTags() 方法

<?php

use App\Models\Post;

$post = Post::createWithoutTags(['title' => 'Test']);

您还可以在不更改其标签的情况下更新模型

<?php

use App\Models\Post;

$post = Post::first();

// Example 1
$post->updateWithoutTags(['title' => 'New Test']);

// Example 2
$post->title = 'New Test';
$post->saveWithoutTags();

删除模型时,您还可以禁用标签删除

<?php

use App\Models\Post;

$post = Post::first();

$post->withoutTags()->delete();

默认情况下,模型将按其表名分组,如果您想更改这一点,可以在模型上定义 $searchType 属性

<?php

use Illuminate\Database\Eloquent\Model;
use MakiDizajnerica\Searcher\Searchable;
use MakiDizajnerica\Searcher\Contracts\Searchable as SearchableContract;

class Post extends Model implements SearchableContract
{
    use Searchable;

    /**
     * @var string
     */
    protected $searchType = 'news';
}

用法

单个模型搜索

当搜索单个模型时,只需将查询字符串传递给 whereTags() 范围。

<?php

use App\Models\Post;

class PostController extends Controller
{
    public function index(Request $request)
    {
        return view('post.index', [
            'posts' => Post::whereTags($request->query('search'))->paginate()
        ]);
    }
}

多模型搜索

<?php

use App\Models\User;
use App\Models\Post;
use MakiDizajnerica\Searcher\Search;
use Illuminate\Database\Eloquent\Builder;

class PostController extends Controller
{
    public function index(Request $request)
    {
        return view('post.index', [
            'search' => (new Search)
                ->addModel(User::class, function (Builder $query) {
                    $query->active()->notAdministrator();
                })
                ->addModel(Post::class, ['latest', 'limit' => 10])
                ->search($request->query('search'))
        ]);
    }
}

方法 addModel() 的第一个参数是将被搜索的模型类的名称。第二个参数是范围,您可以传递上面示例中的 Closurearray。您还可以传递 string,它将表示范围方法名称

<?php

use App\Models\Post;
use MakiDizajnerica\Searcher\Search;

(new Search)->addModel(Post::class, 'latest')->search('test', true);

您还可以将第二个 bool 参数传递给 search() 方法,如果您想进行严格搜索。默认情况下,其设置为 false,这意味着标签将在 LIKE 子句中搜索。

当严格搜索设置为 true 时,只有具有精确标签的模型将被找到。

search() 方法的返回值将是 MakiDizajnerica\Searcher\Collections\SearchResultCollection 实例。

在渲染搜索结果时,您可以这样做

@foreach($search as $type => $results)
    <p>
        {{ $type }}
    </p>

    @foreach($results as $result)
        <div>
            <h1>
                {{ $result->title }}
            </h1>
        </div>
    @endforeach
@endforeach

属性 $results 将代表模型数组,您可以通过循环遍历。

作者

Nemanja Marijanovic (n.marijanovic@hotmail.com)

许可证

版权所有 © 2021, Nemanja Marijanovic n.marijanovic@hotmail.com

保留所有权利。

有关完整的版权和许可信息,请参阅随本包源根一起分发的 LICENSE 文件。