2017 © Pedro Peláez
 

library searchable

Simple fulltext search though Eloquent models

image

masterro/searchable

Simple fulltext search though Eloquent models

  • Friday, May 19, 2017
  • by MasterRO
  • Repository
  • 1 Watchers
  • 7 Stars
  • 66 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 14 Versions
  • 20 % Grown

The README.md

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

Laravel simple FULLTEXT search through multiple Eloquent models

This is a small Laravel package allows you to make a global search though multiple Eloquent models and get ordered by relevance collection of results. It uses MATCH AGAINST MySQL queries., (*2)

Installation

Step 1: Composer

From the command line, run:, (*3)

composer require masterro/laravel-searchable

Step 2: Service Provider

If you do not use laravel package auto-discovery you need to register the service provider, open config/app.php and, within the providers array, append:, (*4)

MasterRO\Searchable\SearchableServiceProvider::class

Usage

Register your search models in AppServiceProvider or create your custom one, (*5)

Searchable::registerModels([
    Post::class,
    Article::class,
    User::class,
]);

Then you should implement MasterRO\Searchable\SearchableContract by each registered model, or it will be skipped and define searchable method, (*6)

public static function searchable(): array
{
    return ['title', 'description'];
}

Make sure you added fulltext indicies to your tables, (*7)

public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();

            $table->string('title');
            $table->longText('description');
            $table->dateTime('published_at')->nullable();

            $table->timestamps();
        });

        DB::statement('ALTER TABLE posts ADD FULLTEXT search(title, description)');
    }

Now you can make search in your controller or where you want, (*8)

public function search(Request $request, Searchable $searchable)
{
    $query = trim($request->input('q'));

    if (mb_strlen($query) < 3) {
        return back()->withInput()->withErrors([
            'search_error' => __('messages.search_error')
        ]);
    }

    return view('search.index')->with('results', $searchable->search($query));
}

Runtime search model switching, (*9)

$result = $this->searchable->searchModel(Post::class, 'consequatur');
$result = $this->searchable->searchModel([Article::class, Post::class], 'consequatur');

Filtering

Model filter

Search results can be filtered by adding the filterSearchResults() in your model (like Eloquent global scope), (*10)

class User extends Model implements SearchableContract
{
    public function posts() {
        return $this->hasMany(Post::class);
    }

    public function filterSearchResults($query) {
        return $query->whereHas('posts', function ($query) {
            $query->where('is_published', true);
        });
    }
}

The example code above will filter the search results and will only return users which have published posts., (*11)

Runtime filter

Search results can be filtered by adding custom filter callback, (*12)

$result = $this->searchable
    ->withFilter(function (Builder $query) {
        return $query->getModel() instanceof Post
            ? $query->where('description', '!=', 'Doloremque iure sequi quos sequi consequatur.')
            : $query;
    })
    ->search('Dolorem');

Disabling model filter

Model filters can be skipped in runtime like Eloquent global scopes., (*13)

$result = $this->searchable->withoutModelFilters()->search('quia est ipsa molestiae hic');

You can specify models to skip filters for, (*14)

$result = $this->searchable
    ->withoutModelFilters(Post::class)
    ->search('quia est ipsa molestiae hic');

or, (*15)

$result = $this->searchable
    ->withoutModelFilters([Article::class, Post::class])
    ->search('quia est ipsa molestiae hic');

Eager load (N+1 issue)

For preventing N+1 issue you can eager load relationships for search results, (*16)

$result = $this->searchable
    ->with([Article::class => 'author'])
    ->search('consequatur');
$result = $this->searchable
    ->with([
        Article::class => [
            'author' => function ($query) {
                return $query->where('active', true);
            },
        ],
    ])
    ->searchModel(Article::class, 'consequatur');

The Versions

19/05 2017

dev-master

9999999-dev

Simple fulltext search though Eloquent models

  Sources   Download

MIT

The Requires

 

by Avatar MasterRO

laravel search fulltext global search

19/05 2017

0.3.2

0.3.2.0

Simple fulltext search though Eloquent models

  Sources   Download

MIT

The Requires

 

by Avatar MasterRO

laravel search fulltext global search

05/04 2017

0.3.1

0.3.1.0

Simple fulltext search though Eloquent models

  Sources   Download

MIT

The Requires

 

by Avatar MasterRO

laravel search fulltext global search

02/04 2017

0.3.0

0.3.0.0

Simple fulltext search though Eloquent models

  Sources   Download

MIT

The Requires

 

by Avatar MasterRO

laravel search fulltext global search

03/03 2017

0.2.0

0.2.0.0

Simple fulltext search though Eloquent models

  Sources   Download

MIT

The Requires

 

by Avatar MasterRO

laravel search fulltext global search

03/03 2017

dev-php5

dev-php5

Simple fulltext search though Eloquent models

  Sources   Download

MIT

The Requires

 

by Avatar MasterRO

laravel search fulltext global search

03/03 2017

0.1.7

0.1.7.0

Simple fulltext search though Eloquent models

  Sources   Download

MIT

The Requires

 

by Avatar MasterRO

laravel search fulltext global search

02/02 2017

0.1.6

0.1.6.0

Simple fulltext search though Eloquent models

  Sources   Download

MIT

The Requires

 

by Avatar MasterRO

laravel search fulltext global search

02/02 2017

0.1.5

0.1.5.0

Simple fulltext search though Eloquent models

  Sources   Download

MIT

The Requires

 

by Avatar MasterRO

laravel search fulltext global search

02/02 2017

0.1.4

0.1.4.0

Simple fulltext search though Eloquent models

  Sources   Download

MIT

The Requires

 

by Avatar MasterRO

laravel search fulltext global search

02/02 2017

0.1.3

0.1.3.0

Simple fulltext search though Eloquent models

  Sources   Download

MIT

The Requires

 

by Avatar MasterRO

laravel search fulltext global search

01/02 2017

0.1.2

0.1.2.0

Simple fulltext search though Eloquent models

  Sources   Download

MIT

The Requires

 

by Avatar MasterRO

laravel search fulltext global search

01/02 2017

0.1.1

0.1.1.0

Simple fulltext search though Eloquent models

  Sources   Download

MIT

The Requires

 

by Avatar MasterRO

laravel search fulltext global search

01/02 2017

0.1.0

0.1.0.0

Simple fulltext search though Eloquent models

  Sources   Download

MIT

The Requires

 

by Avatar MasterRO

laravel search fulltext global search