soraspire/laravel-searchable
Composer 安装命令:
composer require soraspire/laravel-searchable
包简介
A Laravel package that provides a flexible Searchable trait with support for custom operators (like, regex, soundex, fulltext, etc.)
README 文档
README
A lightweight Laravel package that enables advanced searching & sorting directly from URL query parameters.
Designed to let Frontend (FE) fully control search logic without touching Backend code.
Supports custom operators such as like, regex, soundex, fulltext, and more.
Requirements
- PHP 8.1 or higher
- MySQL 8.0+
- Laravel 10.0+
Features
⚡ Fast attribute search with Eloquent
🎛️ Advanced search from request
🔍 Search through single or multiple columns
🔗 Search through (nested) Relationships
📖 Support for Full-Text Search, Soundex Search
↕️ Supports sorting on attributes and first-level relationships
⚙️ Define your own operators
🛡️ Whitelist allowed columns
Installation
You can install the package via composer:
composer require soraspire/laravel-searchable
Publish Config
php artisan vendor:publish --tag=config --provider="Soraspire\\Searchable\\ServiceProvider"
Usage
In your model:
use Soraspire\Searchable\Traits\Searchable; class Post extends Model { use Searchable; protected array $searchable = [ 'title', 'content', 'comments' => ['content'], 'user' => ['id', 'name', 'email'], ]; }
Now you can search with custom operators:
Post::query()->search();
Search directly from URL query params:
GET /posts?title__like=Laravel
In the above query:
title__like=Laravel → find posts with title containing "Laravel"
Search with multiple columns
Search across multiple columns in the same table by passing multiple query conditions.
GET /posts?title__like=Laravel&content__like=Laravel
Search through relationships
Supports searching through relationships (e.g., comments, user). You can combine conditions from both the main model and related models.
GET /posts?title__like=Laravel&content__like=Laravel&comments__content__en=Laravel&comments__user__name__eq=Laravel
FULLTEXT search
Performs a full-text search, allowing more accurate matching across multiple keywords. Works best on columns with a fulltext index.
GET /posts?content__fulltext=Laravel%20Eloquent
SOUNDEX (raw operator)
Searches using the SOUNDEX operator, which matches words that sound similar. Useful for names or terms with different spellings but similar pronunciation.
GET /posts?content__soundex=Laravel
Custom Operators
You can define your own operators inside the config/searchable.php file.
'today' => ['type' => 'raw', 'sql' => 'DATE({col}) = CURDATE()'],
Now you can search using your custom operators:
GET /posts?content__today
Supported Query Operators
| Operator | Type | SQL / Pattern | Description |
|---|---|---|---|
| eq | basic | = | Equal |
| not_eq | basic | != | Not equal |
| lt | basic | < | Less than |
| lte | basic | <= | Less than or equal |
| gt | basic | > | Greater than |
| gte | basic | >= | Greater than or equal |
| cont | like | %{val}% | Contains |
| start | like | {val}% | Starts with |
| end | like | %{val} | Ends with |
| like | like | {val} | Exact like |
| in | in | - | Value in list |
| not_in | not_in | - | Value not in list |
| between | between | - | Value between two values |
| not_between | not_between | - | Value not between two values |
| null | null | - | Is null |
| not_null | not_null | - | Is not null |
| present | present | - | Field exists |
| blank | blank | - | Field is blank |
| year | raw | YEAR({col}) = ? | Match year |
| month | raw | MONTH({col}) = ? | Match month |
| date | raw | DATE({col}) = ? | Match date |
| before_today | raw | DATE({col}) < CURDATE() | Date before today |
| after_today | raw | DATE({col}) > CURDATE() | Date after today |
| today | raw | DATE({col}) = CURDATE() | Today |
| regexp | raw | {col} REGEXP ? | Match regex |
| not_regexp | raw | {col} NOT REGEXP ? | Not match regex |
| soundex | raw | SOUNDEX({col}) = SOUNDEX(?) | Match similar sounding words |
| fulltext | raw | MATCH({col}) AGAINST (? IN BOOLEAN MODE) | Full-text search |
Sorting on attributes
You can sort the result attributes using the order query parameter.
GET /posts?order=title__asc
Sorting on first-level relationships
GET /posts?order=title__asc,comments__user_id__desc
License
The MIT License (MIT). Please see License File for more information.
soraspire/laravel-searchable 适用场景与选型建议
soraspire/laravel-searchable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 08 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「search」 「filter」 「query」 「trait」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 soraspire/laravel-searchable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 soraspire/laravel-searchable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 soraspire/laravel-searchable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel package to retrieve data from Google Search Console
Nova searchable filter for belongsTo relationships.
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
Query filtering in your frontend
Abstraction Layer to index and search entities
Anax Database Active Record module for model classes.
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-28