承接 masterro/laravel-searchable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

masterro/laravel-searchable

Composer 安装命令:

composer require masterro/laravel-searchable

包简介

Simple fulltext search though Eloquent models

README 文档

README

Latest Stable Version Total Downloads Build Status Build status License

Laravel simple FULLTEXT search through multiple Eloquent models

This is a small Laravel package allows you to make a global search though multiple Eloquent models and get ordered by relevance collection of results. It uses MATCH AGAINST MySQL queries.

Installation

Step 1: Composer

From the command line, run:

composer require masterro/laravel-searchable

Step 2: Service Provider

If you do not use laravel package auto-discovery you need to register the service provider, open config/app.php and, within the providers array, append:

MasterRO\Searchable\SearchableServiceProvider::class

Usage

Register your search models in AppServiceProvider or create your custom one

Searchable::registerModels([
    Post::class,
    Article::class,
    User::class,
]);

Then you should implement MasterRO\Searchable\SearchableContract by each registered model, or it will be skipped and define searchable method

public static function searchable(): array
{
    return ['title', 'description'];
}

Make sure you added fulltext indicies to your tables

public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();

            $table->string('title');
            $table->longText('description');
            $table->dateTime('published_at')->nullable();

            $table->timestamps();
        });

        DB::statement('ALTER TABLE posts ADD FULLTEXT search(title, description)');
    }

Now you can make search in your controller or where you want

public function search(Request $request, Searchable $searchable)
{
    $query = trim($request->input('q'));

    if (mb_strlen($query) < 3) {
        return back()->withInput()->withErrors([
            'search_error' => __('messages.search_error')
        ]);
    }
    
    return view('search.index')->with('results', $searchable->search($query));
}

Runtime search model switching

$result = $this->searchable->searchModel(Post::class, 'consequatur');
$result = $this->searchable->searchModel([Article::class, Post::class], 'consequatur');

Filtering

Model filter

Search results can be filtered by adding the filterSearchResults() in your model (like Eloquent global scope)

class User extends Model implements SearchableContract
{
    public function posts() {
        return $this->hasMany(Post::class);
    }

    public function filterSearchResults($query) {
        return $query->whereHas('posts', function ($query) {
            $query->where('is_published', true);
        });
    }
}

The example code above will filter the search results and will only return users which have published posts.

Runtime filter

Search results can be filtered by adding custom filter callback

$result = $this->searchable
    ->withFilter(function (Builder $query) {
        return $query->getModel() instanceof Post
            ? $query->where('description', '!=', 'Doloremque iure sequi quos sequi consequatur.')
            : $query;
    })
    ->search('Dolorem');

Disabling model filter

Model filters can be skipped in runtime like Eloquent global scopes.

$result = $this->searchable->withoutModelFilters()->search('quia est ipsa molestiae hic');

You can specify models to skip filters for

$result = $this->searchable
    ->withoutModelFilters(Post::class)
    ->search('quia est ipsa molestiae hic');

or

$result = $this->searchable
    ->withoutModelFilters([Article::class, Post::class])
    ->search('quia est ipsa molestiae hic');

Eager load (N+1 issue)

For preventing N+1 issue you can eager load relationships for search results

$result = $this->searchable
    ->with([Article::class => 'author'])
    ->search('consequatur');
$result = $this->searchable
    ->with([
        Article::class => [
            'author' => function ($query) {
                return $query->where('active', true);
            },
        ],
    ])
    ->searchModel(Article::class, 'consequatur');

masterro/laravel-searchable 适用场景与选型建议

masterro/laravel-searchable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 277 次下载、GitHub Stars 达 10, 最近一次更新时间为 2020 年 05 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「search」 「laravel」 「fulltext」 「global search」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 masterro/laravel-searchable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 masterro/laravel-searchable 我们能提供哪些服务?
定制开发 / 二次开发

基于 masterro/laravel-searchable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 277
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-05-06