承接 jamal/universal-search-filter 相关项目开发

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

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

jamal/universal-search-filter

Composer 安装命令:

composer require jamal/universal-search-filter

包简介

Universal Search Filter: One-box search across columns, relations, and casts for Laravel.

README 文档

README

A drop-in trait and query builder macro that turns a single search box into rich conditions across selected columns, relations, and casts — with optional Scout integration and relevance ranking.

Why?

Most apps need a "one box" search that just works across names, emails, and related records without hand-writing SQL each time. This package gives you a clean universalSearch() scope plus a whereUniversalSearch() macro you can apply to any Eloquent query.

Features

  • Single text box -> many columns and relations
  • Column weights and simple relevance ordering
  • Fuzzy matching (configurable)
  • Works with relations via whereHas
  • Pagination friendly (paginate() as usual)
  • Optional Laravel Scout adapter (when installed)
  • Zero-config defaults + per-model config overrides

Installation

composer require jamal/universal-search-filter
php artisan vendor:publish --tag=config --provider="Jamal\\UniversalSearchFilter\\UniversalSearchServiceProvider"

This publishes config/universal_search.php to tweak defaults.

Quick Start

In your Eloquent model:

use Illuminate\Database\Eloquent\Model;
use Jamal\UniversalSearchFilter\Traits\UniversalSearchable;

class User extends Model
{
    use UniversalSearchable;

    // Optional per-model config
    protected array $universalSearch = [
        'columns' => ['name', 'email', 'phone'],
        'relations' => [
            'posts' => ['title', 'body'],
        ],
        'weights' => [
            'name' => 3,
            'email' => 2,
            'posts' => 1, // weight applied to each related column
        ],
        'fuzzy' => true,
        'exclude_zero_relevancy' => true
    ];
}

Then in a controller:

$search = request('q');

$users = User::query()
    ->universalSearch($search) // or ->whereUniversalSearch($search)
    ->paginate(15);

The scope adds a computed _universal_relevance column, and orders by it (higher is better).

What is weight?

Weight is simply a number that shows how important a column (or relation) is compared to others when calculating the relevance score.

Here is how it works:

  • When you search, the package checks each column you configured (like name, email, phone) to see if the search tokens match.
  • Each match adds to a computed column called _universal_relevance.
  • The weight you assign decides how many “points” a match in that column adds.

For example:

protected array $universalSearch = [
        'columns' => [
            'name',
            'email',
            'status'
        ],
        'weights' => [
            'name' => 5,   // highest importance
            'email' => 3,
            'status' => 1, // least important
        ],
];

If you search for "alice":

  • A hit in the name column adds 5 points.
  • A hit in the email column adds 3 points.
  • A hit in the status column adds only 1 point.

The query then orders by _universal_relevance DESC, so results with matches in high-weighted columns appear first.

So weight = ranking priority. It does not remove columns from search, it just decides which matches push a record higher in the list.

Using the Query Builder Macro

If you are on the Query Builder directly (no model) and want a simple multi-column search:

DB::table('users')
    ->whereUniversalSearchOn($search, ['name', 'email', 'phone'])
    ->paginate(20);

Scout (Optional)

If you have Laravel Scout and your model uses the Searchable trait, enable the setting in config/universal_search.php:

'use_scout_when_available' => true,

The scope will route searches via Scout, then hydrate your Eloquent query based on the keys returned. If no results are found, it falls back to the Eloquent driver.

Configuration

config/universal_search.php:

  • columns: default columns when a model does not provide its own list
  • relations: relation => [columns] pairs
  • weights: increase importance of certain columns/relations
  • fuzzy: true uses %token%, false uses prefix token%
  • min_token_length: ignore very short tokens
  • use_scout_when_available: prefer Scout when the model is searchable

Notes on Relevance

The package builds a simple relevance score with a CASE WHEN column LIKE ? THEN weight sum across tokens and columns. This is portable and works on MySQL, Postgres, and SQLite. For advanced ranking, consider switching to Scout with a dedicated engine (e.g., Meilisearch, Algolia, TNTSearch).

Examples

Search users by "john doe" across users and their posts:

$users = User::universalSearch('"john doe"')
    ->with('posts')
    ->paginate();

Provide options at call site:

$users = User::universalSearch($search, [
    'fuzzy' => false, // switch to prefix matching
    'min_token_length' => 3,
]);

Use only the macro with manual columns:

$orders = Order::query()
    ->whereUniversalSearch($search, ['columns' => ['reference', 'status']])
    ->paginate();

Testing (stub)

You can write tests by making a few models with the trait and seeding a handful of rows, then asserting counts and order of _universal_relevance.

License

MIT

Tailored Defaults

Out of the box, the config ships with useful defaults:

  • columns: name, email, title, phone, reference, status
  • weights: name (5), email (4), title/reference (3), status (1)
  • Empty relations so you can opt-in per model

Override at the model level by defining $universalSearch or globally by publishing the config.

Running Tests

This package uses PHPUnit with Orchestra Testbench.

composer install
composer test

The test suite provisions an in-memory SQLite database, runs simple migrations, seeds demo data, then verifies:

  • token parsing for quoted phrases
  • column and relation search results
  • relevance column is appended and used for ordering
  • macro usage on the plain query builder
  • fuzzy vs prefix matching

jamal/universal-search-filter 适用场景与选型建议

jamal/universal-search-filter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 jamal/universal-search-filter 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-08