theokbokki/laravel-filter-search 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

theokbokki/laravel-filter-search

Composer 安装命令:

composer require theokbokki/laravel-filter-search

包简介

A powerful syntax for filtering models through a search box (Similar to Github projects)

README 文档

README

Warning

This package wasn't yet tested in production use at your own risks. Tests should arrive soon.

Why?

I was making a website that needed a lot of filtering flexibility and a simple search wouldn't do. I could have made a bunch of filters with selects, checkboxes etc, but that would quickliy have become messy.

Then I remembered how github projects does it and it solved all the problems at once!

Requirements

  • PHP 8.0+
  • Laravel 9.0+

Installing

You can install the package via composer

composer require theokbokki/laravel-filter-search

Using

The model you want to make searchable should have the trait HasFilterSearch and it should implement these 2 methods:

public static function defaultSearchFields(): array
{
    return [
        // the list of the model fields you want to be able to seach normally.
    ];
}

public static function allowedSearchFields(): array
{
    return [
        // the list of the model fields you want to allow filtering on. 
    ];
}

Then when you want to search (for example in a controller), you use the handleFilterSearch method

$query = Snippet::handleFilterSearch($searchTerm);

The syntax

The syntax was heavily inspired by the one from Github projects, here's a rundown of the possibilities. One thing to note is that case isn't taken into account, so "TEST" and "test" are the same.

Regular search

You can simply put a search term like normal and get what you want

Search terms Result
test Will return the records containg the string "test" in the fields specified in defaultSearchFields()

Field search

You can use all the fields speicifed in allowedSearchFields() to make the search more precise

Search terms Result
title:Test Will return the records containg the string "test" in the title field

Combining filters

You can freely mix and match search terms and filters to get very precise results

Search terms Result
title:Foo published:true Bar Will return the records containg the string "Foo" in the title field, with published = true and Bar in the fields specified in defaultSearchFields()

Reversing the search

You can also add an hyphen before the field or keyword to reverse the search

Search terms Result
-title:Test Will return any record not containg the string "test" in the title field
-test Will return any record not containg the string "test" in the fields specified in defaultSearchFields()

What if my search contains space, commas or colons?

You can add double quotes around your search to handle space, commas and colons.

Search terms Result
title:"Sentence: with spaces, colons and commas" Will return any record containg the string "Sentence: with spaces, colons and commas" in the title field

theokbokki/laravel-filter-search 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-13