2017 © Pedro Peláez
 

library searchable

Eloquent model search trait.

image

phpffcms/searchable

Eloquent model search trait.

  • Monday, January 8, 2018
  • by zenn
  • Repository
  • 1 Watchers
  • 2 Stars
  • 200 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 167 Forks
  • 0 Open issues
  • 43 Versions
  • 6 % Grown

The README.md

Searchable, a search trait for Laravel

Searchable is a trait for Laravel 4.2+ and Laravel 5.0 that adds a simple search function to Eloquent Models., (*1)

Searchable allows you to perform searches in a table giving priorities to each field for the table and it's relations., (*2)

This is not optimized for big searches, but sometimes you just need to make it simple (Although it is not slow)., (*3)

Installation

Simply add the package to your composer.json file and run composer update., (*4)

"nicolaslopezj/searchable": "1.*"

Usage

Add the trait to your model and your search rules., (*5)

use Nicolaslopezj\Searchable\SearchableTrait;

class User extends \Eloquent
{
    use SearchableTrait;

    /**
     * Searchable rules.
     *
     * @var array
     */
    protected $searchable = [
        'columns' => [
            'users.first_name' => 10,
            'users.last_name' => 10,
            'users.bio' => 2,
            'users.email' => 5,
            'posts.title' => 2,
            'posts.body' => 1,
        ],
        'joins' => [
            'posts' => ['users.id','posts.user_id'],
        ],
    ];

    public function posts()
    {
        return $this->hasMany('Post');
    }

}

Now you can search your model., (*6)

// Simple search
$users = User::search($query)->get();

// Search and get relations
// It will not get the relations if you don't do this
$users = User::search($query)
            ->with('posts')
            ->get();

Search Paginated

As easy as laravel default queries, (*7)

// Search with relations and paginate
$users = User::search($query)
            ->with('posts')
            ->paginate(20);

Mix queries

Search method is compatible with any eloquent method. You can do things like this:, (*8)

// Search only active users
$users = User::where('status', 'active')
            ->search($query)
            ->paginate(20);

Custom Threshold

The default threshold for accepted relevance is the sum of all attribute relevance divided by 4. To change this value you can pass in a second parameter to search() like so:, (*9)

// Search with lower relevance threshold
$users = User::where('status', 'active')
            ->search($query, 0)
            ->paginate(20);

The above, will return all users in order of relevance., (*10)

How does it work?

Searchable builds a query that search through your model using Laravel's Eloquent. Here is an example query, (*11)

Eloquent Model:

use Nicolaslopezj\Searchable\SearchableTrait;

class User extends \Eloquent
{
    use SearchableTrait;

    /**
     * Searchable rules.
     *
     * @var array
     */
    protected $searchable = [
        'columns' => [
            'first_name' => 10,
            'last_name' => 10,
            'bio' => 2,
            'email' => 5,
        ],
    ];

}

Search:

$search = User::search('Sed neque labore', null, true)->get();

Result:

select `users`.*, 

-- If third parameter is set as true, it will check if the column starts with the search
-- if then it adds relevance * 30
-- this ensures that relevant results will be at top
(case when first_name LIKE 'Sed neque labore%' then 300 else 0 end) + 

-- For each column you specify makes 3 "ifs" containing 
-- each word of the search input and adds relevace to 
-- the row

-- The first checks if the column is equal to the word,
-- if then it adds relevance * 15
(case when first_name LIKE 'Sed' || first_name LIKE 'neque' || first_name LIKE 'labore' then 150 else 0 end) + 

-- The second checks if the column starts with the word,
-- if then it adds relevance * 5
(case when first_name LIKE 'Sed%' || first_name LIKE 'neque%' || first_name LIKE 'labore%' then 50 else 0 end) + 

-- The third checks if the column contains the word, 
-- if then it adds relevance * 1
(case when first_name LIKE '%Sed%' || first_name LIKE '%neque%' || first_name LIKE '%labore%' then 10 else 0 end) + 

-- Repeats with each column
(case when last_name LIKE 'Sed' || last_name LIKE 'neque' || last_name LIKE 'labore' then 150 else 0 end) + 
(case when last_name LIKE 'Sed%' || last_name LIKE 'neque%' || last_name LIKE 'labore%' then 50 else 0 end) +
(case when last_name LIKE '%Sed%' || last_name LIKE '%neque%' || last_name LIKE '%labore%' then 10 else 0 end) + 

(case when bio LIKE 'Sed' || bio LIKE 'neque' || bio LIKE 'labore' then 30 else 0 end) + 
(case when bio LIKE 'Sed%' || bio LIKE 'neque%' || bio LIKE 'labore%' then 10 else 0 end) + 
(case when bio LIKE '%Sed%' || bio LIKE '%neque%' || bio LIKE '%labore%' then 2 else 0 end) + 

(case when email LIKE 'Sed' || email LIKE 'neque' || email LIKE 'labore' then 75 else 0 end) + 
(case when email LIKE 'Sed%' || email LIKE 'neque%' || email LIKE 'labore%' then 25 else 0 end) + 
(case when email LIKE '%Sed%' || email LIKE '%neque%' || email LIKE '%labore%' then 5 else 0 end) 

as relevance 
from `users` 
group by `id` 

-- Selects only the rows that have more than
-- the sum of all attributes relevances and divided by 4
-- Ej: (20 + 5 + 2) / 4 = 6.75
having relevance > 6.75 

-- Orders the results by relevance
order by `relevance` desc

Contributing

Anyone is welcome to contribute. Fork, make your changes, and then submit a pull request., (*12)

Support via Gittip, (*13)

The Versions

08/01 2018

dev-master

9999999-dev

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez
by Mihail Pyatinsky

database eloquent search model searchable ffcms

11/02 2017

1.9.6

1.9.6.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez
by Mihail Pyatinsky

database eloquent search model searchable ffcms

05/01 2017

1.9.5

1.9.5.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez
by Mihail Pyatinsky

database eloquent search model searchable ffcms

20/01 2016

1.7.3

1.7.3.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez
by Mihail Pyatinsky

database eloquent search model searchable ffcms

07/01 2016

1.7.2

1.7.2.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

07/01 2016

1.7.1

1.7.1.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

01/12 2015

1.7.0

1.7.0.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

23/11 2015

1.6.2

1.6.2.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

28/10 2015

1.6.1

1.6.1.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

20/10 2015

1.6.0

1.6.0.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

20/09 2015

1.5.15

1.5.15.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

05/08 2015

1.5.14

1.5.14.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

05/08 2015

1.5.13

1.5.13.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

17/06 2015

1.5.12

1.5.12.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

03/06 2015

1.5.11

1.5.11.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

01/06 2015

1.5.10

1.5.10.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

20/05 2015

1.5.9

1.5.9.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

13/05 2015

1.5.8

1.5.8.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

12/05 2015

1.5.7

1.5.7.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

11/05 2015

1.5.6

1.5.6.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

08/05 2015

1.5.5

1.5.5.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

22/04 2015

1.5.4

1.5.4.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

20/04 2015

1.5.3

1.5.3.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

14/04 2015

1.5.1

1.5.1.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

14/04 2015

1.5.2

1.5.2.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

05/04 2015

1.5.0

1.5.0.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

17/03 2015

1.4.5

1.4.5.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

07/03 2015

1.4.4

1.4.4.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

07/02 2015

1.4.3

1.4.3.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

04/02 2015

1.4.2

1.4.2.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

19/11 2014

1.4.1

1.4.1.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

19/11 2014

1.4

1.4.0.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

06/11 2014

dev-sqlsrv

dev-sqlsrv

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

29/10 2014

1.3

1.3.0.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

25/10 2014

1.2.3

1.2.3.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

23/10 2014

1.2.2

1.2.2.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

22/10 2014

1.2.1

1.2.1.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

21/10 2014

1.2.0

1.2.0.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

database laravel eloquent search model searchable

10/10 2014

1.1.2

1.1.2.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

laravel eloquent search searchable

09/10 2014

1.1.1

1.1.1.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

laravel eloquent search searchable

08/10 2014

1.1.0

1.1.0.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

laravel eloquent search searchable

17/06 2014

1.0.0

1.0.0.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

laravel eloquent search searchable

17/06 2014

0.1.2

0.1.2.0

Eloquent model search trait.

  Sources   Download

MIT

The Requires

 

by Nicolas Lopez

laravel eloquent search searchable