Skip to content

Easily add fulltext search capabilities to your Eloquent models

Notifications You must be signed in to change notification settings

filippotoso/fulltext

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Fulltext

A simple way to add fulltext capabilities to your Eloquent models (just for MySQL for the moment).

Requirements

  • PHP 5.6+
  • Laravel 5.0+

Installing

Use Composer to install it:

composer require filippo-toso/fulltext

Using It

Create a fulltext index in your migrations:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

use FilippoToso\Fulltext\FullText;

class AddIndexesToUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // Create a fulltext index in the users table for the first_name and last_name columns
        FullText::fullTextIndex('users', ['first_name', 'last_name']);

        // You can also specify a name for the index with the third parameter
        FullText::fullTextIndex('users', ['city', 'address', 'country'], 'search_address');

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {

        Schema::table('users', function (Blueprint $table) {
            $table->dropIndex(['first_name', 'last_name']);
            $table->dropIndex('search_address');
        });

    }

}

Enable fulltext functions within the User model (for instance):

namespace App;

use Illuminate\Database\Eloquent\Model;
use FilippoToso\FullText\Traits\FullText;

class User extends Model
{
    use FullText;
}

Use the FullText trait's features:

// Use the whereMatch() scope to search for all users named "filippo"
$users = App\User::select('*')
                ->whereMatch(['first_name', 'last_name'], 'filippo')
                ->get();

// Include in the result the score column with the matching score
$users = App\User::select('*')
                ->whereMatch(['first_name', 'last_name'], 'filippo')
                ->selectMatchScore(['first_name', 'last_name'], 'filippo')
                ->get();

// You can also include the name of the score column with the third parameter of selectMatchScore()
$users = App\User::select('*')
                ->whereMatch(['first_name', 'last_name'], 'filippo')
                ->selectMatchScore(['first_name', 'last_name'], 'filippo', 'matching_score')
                ->get();

About

Easily add fulltext search capabilities to your Eloquent models

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages