Download the PHP package djl997/laravel-search-query-builder without Composer
On this page you can find all versions of the php package djl997/laravel-search-query-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download djl997/laravel-search-query-builder
More information about djl997/laravel-search-query-builder
Files in djl997/laravel-search-query-builder
Package laravel-search-query-builder
Short Description A `Illuminate\Database\Query\Builder` macro to easily search on multiple database columns.
License MIT
Informations about the package laravel-search-query-builder
Laravel Search Query Builder
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.
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:
Usage
Basic search term
The most basic call to the search
method requires two arguments:
- an array of columns,
- the search value.
For example, the following query retrieves users where the name
column or the value of the bio
column is like "Cesar".
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.
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.
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.
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.
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:
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.