Download the PHP package tasdan/column-searchable without Composer
On this page you can find all versions of the php package tasdan/column-searchable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package column-searchable
Searching in model's columns for Laravel 5.5-8
A package for searching in your eloquent model's column, with relations in Laravel.
Table of contents
- Installation
- Composer
- Service provider
- Configuration
- Usage
- Configurations
- Config examples
- Blade Extension
- Searchable input field
- Searchable script
- Pagination
- Full example
- Library Note
- License
Installation
Composer
Install the package with composer require command:
Service provider
Next, add the new provider to the providers
array in config/app.php
(only when Laravel < 5.5):
Configuration
You can publish the configuration file with the following command:
After that, you can change the default configurations in file.
Usage
To use searchable scope, you have to add Searchable trait inside the Eloquent models.
Searchable trait adds Searchable scope to the models.
Then define $searchable
array in model's definition (check the configuration section to more info):
After you defined the $searchable
array, you can use searchable
scope in your controllers:
You can execute a search function when the requested URL has one of the defined $searchable
key, see this example:
Note: the parameter values in URL are URL-encoded.
Configurations
You can configure search field as much as you want, with the following pattern:
- search_field_name_1: the name of this search field, can be anything, or the column name where you want to search
- relation: the related table name, can be omitted, or the current model's table name
- columns: the column name(s) where you want to search, use an array, when you want to search in multiple columns
- type: the column type, must be string or int (if string, the query be like 'LIKE %xyz%'), can be omitted
Config examples
Blade Extension
There are two blade extension for you to use searchable functions
Searchable input field
In blade files you can use the @searchablefield() extension to automatically generated search input form.
Note: the search-input class automatically added to this generated fields, to able to work the @searchablescript() extension.
This first parameter (field_type) must be one of the following:
- text
- select
The second parameter is the searchable field name, which is already defined in model's $searchable
array.
The third parameter is the title-placeholder value of the field. The fourth parameter depends on the type parameter:
- if type is text, this is an array with additional form values (optional)
- if type is select, this is an array with select options key-value pairs
The fifth parameter is optional when type is select, and this is an array with additional form values.
Possible examples and usages of this balde extension:
Searchable script
In blade files you can use the @searchablescript() extension to automatically generated search Javascript functions.
After this you can call search()
function to execute a search request.
Example use of @searchablescript() extension:
Note: you have to call @searchablescript() extension inside script tag, because this extension returns only with JS functions code. In this way, you can use CSP-nonce on this include.
Pagination
If you use pagination in blade, you have to update the pagination links, to keep searching parameters in URL when page change.
Full example
TODO
Library Note:
This is my first Laravel package, so maybe there is some problem with that, but I will handle those as soon as possible.
License
This package is free software distributed under the terms of the MIT license. Enjoy!
All versions of column-searchable with dependencies
illuminate/support Version 5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0
illuminate/database Version 5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0