Download the PHP package whitecube/laravel-search-builder without Composer
On this page you can find all versions of the php package whitecube/laravel-search-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download whitecube/laravel-search-builder
More information about whitecube/laravel-search-builder
Files in whitecube/laravel-search-builder
Package laravel-search-builder
Short Description A package to build fast, index-friendly search queries for Laravel
License
Informations about the package laravel-search-builder
Search Builder for Laravel
A package to build fast, index-friendly search queries for Laravel.
What does it do?
The purpose of this package is to allow you to build multi-condition search queries with scores, and make use of covering indexes to be super fast. This means simply using this package as shown is not everything – you need to have a well designed database, with properly designed indexes for your searches, to get good results. To learn more about this topic, we recommend watching the free PlanetScale MySQL for Developers course.
Installation
How to structure your query
To make your search queries extremely fast, the search builder will pack all of your conditions in a subquery that will aim to hit as many covering indexes as possible, in order to build an aggregated table that only contains ids of the pertinent models (along with a score, more on this later). This aggregated table will then be used to filter down the actual table with an inner join. This means that the processing of your search logic is done entirely on your indexes, and the full table is only accessed at the end, which dramatically speeds everything up.
However, the package can not detect your database structure, so it is your responsibility to create your indexes correctly, and in such a way that your search condition queries will not have to access your main tables' data.
Here's an example of what we're looking to achieve, in raw SQL. Given that we have a products table, and we want to search it by reference and by name, and prioritise the reference over the name:
The search builder instance
You can get a search builder instance just by passing it the model you want to search.
Or, if your model uses the HasSearchBuilder
trait, you can easily get a search builder instance this way, which allows you to cleanly chain your condition methods later.
Defining search conditions
Once you have a search builder instance, you can use it to define your search conditions, by passing eloquent builder instances to the search
method.
The score is optional and will be automatically computed if missing, using the order in which the conditions are defined, with the highest score on top.
You can easily search on related tables. Remember to only select the column that references the id of the table you're searching.
If you wish to split the search terms on spaces, dashes, dots and underscores, and perform individual queries on each term, you can call the splitTerms
method.
Getting the results
After defining your conditions, you can get the collection of results by calling the get
method.
Or, if you need to do more work on the query yourself, you can get the query builder instance.
💖 Sponsorships
If you are reliant on this package in your production applications, consider sponsoring us! It is the best way to help us keep doing what we love to do: making great open source software.
Contributing
Feel free to suggest changes, ask for new features or fix bugs yourself. We're sure there are still a lot of improvements that could be made, and we would be very happy to merge useful pull requests.
Thanks!
Unit tests
When adding a new feature or fixing a bug, please add corresponding unit tests. The current set of tests is limited, but every unit test added will improve the quality of the package.
Run the test suite by calling ./vendor/bin/pest
.
Made with ❤️ for open source
At Whitecube we use a lot of open source software as part of our daily work. So when we have an opportunity to give something back, we're super excited!
We hope you will enjoy this small contribution from us and would love to Twitter for more updates!