2017 © Pedro Peláez
 

library laravel-searchable

A Simple trait search for Laravel Models

image

dlimars/laravel-searchable

A Simple trait search for Laravel Models

  • Wednesday, February 28, 2018
  • by dlimars
  • Repository
  • 2 Watchers
  • 4 Stars
  • 298 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 30 % Grown

The README.md

Laravel Searchable

Build Status Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

a simple trait to use with your Laravel Models, (*2)

Installation

open terminal and run: composer require dlimars/laravel-searchable, (*3)

Configuration

just add in your models, (*4)

    class MyModel extends Model {
        use Dlimars\LaravelSearchable\Searchable;
        private $searchable = [
            'name'          => 'LIKE',
            'id'            => 'MATCH',
            'created_at'    => 'BETWEEN'
        ];
    }

Usage

just call search() method in model, (*5)

    $filters = [
        'name'          => 'foo bar',
        'id'            => '10',
        'created_at'    => ['2010-01-01 00:00:00', '2015-01-01 23:59:59']
    ];

    $users = User::search($filters)->get();
    // produces $query->where('name', 'LIKE', '%foo%')
    //                ->where('name', 'LIKE', '%bar%')
    //                ->where('id', '10')
    //                ->where('created_at', '>=', '2010-01-01 00:00:00')
    //                ->where('created_at', '<=', '2015-01-01 23:59:59')

    $filters = [
        'created_at'    => ['2010-01-01 00:00:00', null]
    ];
    //  produces $query->where('created_at', '>=', '2010-01-01 00:00:00')

    $filters = [
        'created_at'    => [null, '2015-01-01 23:59:59']
    ];
    //  produces $query->where('created_at', '<=', '2015-01-01 23:59:59')

you can also use with request, (*6)

    $users = User::search($request()->all())->get();

Default Operators

    'LIKE'      // produces $query->where('field', 'LIKE', '%{$value}%')
    'MATCH'     // produces $query->where('field', $value)
    'BETWEEN'   // produces $query->where('field', '>=', $value[0])
                //                ->where('field', '<=', $value[1])

Custom Operators

You can create a custom scope in your models, and call as operator., (*7)

Example of class:, (*8)

    class Customer extends EloquentModel
    {
        use Searchable;

        public $fillable = [
            'firstname',
            'lastname'
        ];

        public $searchable = [
            'id'    => 'MATCH',
            'name'  => 'myCustomSearchByName'
        ]

        public function scopeMyCustomSearchByName($queryBuilder, $name)
        {
            $name = str_slug($name, "%");

            $queryBuilder->where(
                \DB::raw('CONCAT(firstname, " ", lastname'),
                'LIKE',
                "%{$name}%"
            );
        }
    }

and call, (*9)

    $filters   = ['name' => 'Foo bar example'];
    $customers = Customer::search($filters)->get();

The Versions

28/02 2018

dev-master

9999999-dev

A Simple trait search for Laravel Models

  Sources   Download

The Development Requires

by Daniel Lima

28/02 2018

1.1.1

1.1.1.0

A Simple trait search for Laravel Models

  Sources   Download

The Development Requires

by Daniel Lima

21/08 2015

1.1.0

1.1.0.0

A Simple trait search for Laravel Models

  Sources   Download

The Development Requires

by Daniel Lima

21/08 2015

1.0.0

1.0.0.0

A Simple trait search for Laravel Models

  Sources   Download

The Development Requires

by Daniel Lima