Download the PHP package ajcastro/searchable without Composer
On this page you can find all versions of the php package ajcastro/searchable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ajcastro/searchable
More information about ajcastro/searchable
Files in ajcastro/searchable
Package searchable
Short Description Pattern-matching search for Laravel eloquent models.
License MIT
Informations about the package searchable
Searchable
Pattern-matching search for Laravel eloquent models.
- Currently supports MySQL only.
- Helpful for complex table queries with multiple joins and derived columns.
- Fluent columns definitions.
Demo Project
See demo project.
Overview
Simple setup for searchable model and can search on derived columns.
Imagine we have an api for a table or list that has searching and column sorting and pagination. This is a usual setup for a table or list. The internal explanations will be available on the documentation below. Our api call may look like this:
http://awesome-app.com/api/posts?per_page=10&page=1&sort_by=title&descending=true&search=SomePostTitle
Your code can look like this:
Documentation
Installation
Searchable Model
If you want to sort by relevance, call method sortByRelevance()
after search()
method.
This will addSelect field sort_index
which will be used to order or sort by relevance.
Example:
Set searchable configurations on runtime.
Easy Sortable Columns
You can define columns to be only sortable but not be part of search query constraint.
Just put it under sortable_columns
as shown below .
This column can be easily access to put in orderBy
of query builder. All searchable columns are also sortable columns.
Custom Search String Parser - Exact Search Example
Override the deafultSearchQuery
in the model like so:
You may also check the build query by dd-ing it:
which may output to
Using derived columns for order by and where conditions
Usually we have queries that has a derived columns like our example for Post
's author_full_name
.
Sometimes we need to sort our query results by this column.
which may output to
Helper methods available
TableColumns::get() [static]
- Get the table columns.
isColumnValid
- Identifies if the column is a valid column, either a regular table column or derived column.
- Useful for checking valid columns to avoid sql injection especially in
orderBy
query, see post.
enableSearchable
- Enable the searchable behavior.
disableSearchable
- Disable the searchable behavior.
- Calling
search()
method will not perform a search.
setSearchable
- Set or override the model's
$searchable
property. - Useful for building searchable config on runtime.
addSearchable
- Add columns or joins in the model's
$searchable
property. - Useful for building searchable config on runtime.
Warning
Calling select()
after search()
will overwrite sort_index
field, so it is recommended to call select()
before search()
. Or you can use addSelect()
instead.
Credits
- Ray Anthony Madrona @raymadrona, for the tips on using MySQL
LOCATE()
for sort relevance. - nicolaslopezj/searchable, for the
$searchable
property declaration style.