djl997/laravel-search-query-builder 问题修复 & 功能扩展

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

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

djl997/laravel-search-query-builder

Composer 安装命令:

composer require djl997/laravel-search-query-builder

包简介

A `Illuminate\Database\Query\Builder` macro to easily search on multiple database columns.

README 文档

README

Latest Version on Packagist Total Downloads Software License

Laravel Search Query Builder is a Illuminate\Database\Query\Builder macro to easily search on multiple database columns and model relationships.

Let's imagine a case where you want to return all users with something in their name or bio field.

$search = 'Cesar';

// vanilla laravel
User::where('name', 'LIKE', '%'. $search .'%')->orWhere('bio', 'LIKE', '%'. $search .'%')->get();

// laravel with laravel-search-query-builder package
User::search(['name', 'bio'], $search)->get();

This example is not very difficult to achieve in Laravel, but it can grow very quickly, especially if you also want to search in model relationships like posts or comments for example.

Let's see how the Laravel Search Query Builder package can improve this situation.

Requirements

Laravel Search Query Builder requires PHP 8+ and Laravel 9+.

Installation

You can install the package via composer:

composer require djl997/laravel-search-query-builder

Usage

Basic search term

The most basic call to the search method requires two arguments:

  1. an array of columns,
  2. the search value.

For example, the following query retrieves users where the name column or the value of the bio column is like "Cesar".

$search = 'Cesar';

User::search(['name', 'bio'], $search)->get();

This will generate a query where "Cesar" is LIKE the name or LIKE bio column. For clarification, the search term is wrapped between %...% to check if (a part of) the string is present.

Multiple search terms

You can also perform multiple searches at once by separating them with a komma.

$search = 'Cesar, Victoria';

User::search(['name', 'bio'], $search)->get();

This will generate a query for each part of the search term, exploded on a , by default.

Change the separator

If you want to change the delimiter, you can do so by changing the third argument.

User::search(['name', 'bio'], 'Cesar Victoria', '|')->get();

This will generate a query for each part of the search term, exploded on a |.

Search multiple relationships

As you probably know, the whereHas method gives you a lot of power to define additional query constraints. And you guessed it, you can use the search method here as well.

For example, the following query will return all users with "Laravel is cool" in their bios + all users who write posts that include this phrase in the title or body.

$search = 'Laravel is cool';

User::search(['bio'], $search)
    ->orWhereHas('posts', function($query) use ($search) {
        $query->search(['title', 'body'], $search);
    })
    ->get();

Combine search with other queries

Since the search method is developed to be a macro and extend the Illuminate\Database\Query\Builder it can be used inline with any other Builder methods such as whereIn, withTrashed or orderBy, or your very own macros.

$search = 'Laravel is cool';

User::whereIn('role', ['author', 'reviewer'])
    ->where(function($query) { //see note below
        ->search(['bio'], $search)
        ->orWhereHas('posts', function($query) use ($search) {
            $query
                ->search(['title', 'body'], $search)
                ->whereNotNull('published_at');
        })
        ->orWhereHas('comments', function($query) use ($search) {
            $query->search(['body'], $search);
        })
    })
    ->withTrashed()
    ->orderBy('name')
    ->get();

This will return a ordered list of users with author-, or reviewer roles (deleted AND non-deleted), with "Laravel is cool" in their bios, published posts or comments, ordered by name.

Note! The search query is wrapped in a where query, because you most likely don't want the 'orWheres' to bypass any other filters.

Dynamic columns search

This is a short-sighted example of how you can also make the columns dynamic:

<input type="text" name="search" value="Laravel is cool"/>
<input type="checkbox" name="userColumns[]" value="name"/>
<input type="checkbox" name="userColumns[]" value="bio"/>
$search = $request->input('search');
$columns = $request->input('userColumns');

User::search($columns, $search)->get();

Changelog

Please see the GitHub releases for more information on what has changed recently.

Contributing

Contributions or ideas are welcome.

License

The MIT License (MIT). Please see License File for more information.

djl997/laravel-search-query-builder 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-04-13