bigdropinc/laravel-simple-search

Laravel package for search.

dev-master 2020-01-27 16:01 UTC

This package is not auto-updated.

Last update: 2024-04-16 13:28:51 UTC


README

#Laravel Simple Search

##Requirements

Make sure all dependencies have been installed before moving on:

Pull the package via Composer:

The preferred way to install this extension is through composer.

Either run

composer require bigdropinc/laravel-simple-search "1.0.0"

or add

"bigdropinc/laravel-simple-search": "1.0.0"

to the require section of your composer.json file.

Usage

Once the extension is installed, simply use it in your code by:

UserSearch::apply(User:class, request()->all());

Filters

All attributes that should be used described in property fillable

protected $fillable = [
   'id',
   'first_name',
   'last_name',
];

By default for each attribute applied condition =

Example:

protected $fillable = [
   'id',
   'name' => 'first_name', //alias:  name - search attr, first_name - db attr
   'last_name',
];

Equivalent

User::where('first_name', $firstNameValue)
   ->where('last_name', $lastNameValue)
   ->where('id', $userIdValue)

Сustom filters

pulic function id($value) {
   $this->query->where('id', '>', $value);
}

Cast

Used default Laravel attribute casting. By default use string cast

protected $cast = [
  'id' => 'integer',	
];

Sort

Default sort

protected $defaultSort = 'first_name';

Ascending order by first_name: sort=first_name

Descending order by first_name: sort=-first_name | hyphen (-) in the start