2017 © Pedro Peláez
 

library eloquent-search-map

An Eloquent search macro for effortlessly searching models

image

aviator/eloquent-search-map

An Eloquent search macro for effortlessly searching models

  • Wednesday, November 29, 2017
  • by danielsdeboer
  • Repository
  • 1 Watchers
  • 0 Stars
  • 19 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 36 % Grown

The README.md

Build Status Latest Stable Version License, (*1)

Overview

Add Eloquent query search constraints effortlessly with this Eloquent macro., (*2)

This is especially handy when you find yourself building simple, optional searches often:, (*3)

$model::when($request->something, function (Builder $query) {
    return $query->where('column', 'like', '%' . $request->something . '%'); 
})->get();

With this package you can do this instead:, (*4)

$model::search(['something', 'otherthing'])->get();

You may create an optional column map on your model, as well as mapping request properties on the fly., (*5)

Installation

Via Composer:, (*6)

composer require aviator/eloquent-search-map

In your config/app.php add Aviator\Search\ServiceProvider::class to the providers array:, (*7)

'providers' => [
    ...
    Aviator\Search\ServiceProvider::class,
],

Testing

Via Composer:, (*8)

composer test

Usage

Model Setup

To start, make the model you want to search implement the Searchable contract and use the SearchableTrait., (*9)

class User extends Model implements Searchable
{
    use SearchableTrait;

    // ..etc
}

Then set a searches array property on your model containing your searchable columns. To search in the email column using $model::search(['email']):, (*10)

protected $searches = [
    'email'
];

To search in the email column using $model::search(['alias']), (*11)

protected $searches = [
    'alias' => 'email'
];

Request Aliases

By default, the search builder assumes the column name or alias matches the request data. So if you call $model::search(['something']), it will look for request('something')., (*12)

Of course you can specify the request property name manually:, (*13)

$model::search(['email' => 'user_email'])->get();

This tells the search builder to look for the email request data in request('user_email') instead of the default., (*14)

Custom Requests

If you need to pass a custom request into the macro, use the second parameter, which accepts an object extending Illuminate\Http\Request:, (*15)

$model::search(['term'], $request)->get();

Of course, this is completely optional. If a request isn't provided, it will be retrieved from the container., (*16)

If you want to query related models, you can! Use dot notation:, (*17)

protected $searches = [
    'relation.column'
];

This will look for a relation method called company() and add a whereHas constraint to the query. For instance:, (*18)

$users = User::search(['company.city'])->get();

This will look on the User model for a relation company() and search in the city attribute of that model., (*19)

By default we assume that the request will have the same property, snake cased. For the above query constraint the search builder will look for request('company_city')., (*20)

This can also be mapped:, (*21)

$users = User::search(['company.city' => 'city'])->get();

The search builder will now look for request('city') instead., (*22)

Other Stuff

License

This package is licensed with the MIT License (MIT)., (*23)

The Versions

29/11 2017

dev-master

9999999-dev https://github.com/danielsdeboer/eloquent-search-map

An Eloquent search macro for effortlessly searching models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel php eloquent search php7

29/11 2017

0.1.1

0.1.1.0 https://github.com/danielsdeboer/eloquent-search-map

An Eloquent search macro for effortlessly searching models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel php eloquent search php7

28/11 2017

0.1.0

0.1.0.0 https://github.com/danielsdeboer/eloquent-search-map

An Eloquent search macro for effortlessly searching models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel php eloquent search php7