1. Go to this page and download the library: Download muhaimenul/lara-search library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
muhaimenul / lara-search example snippets
use Muhaimenul\Larasearch\Traits\LaraSearch;
class User extends Model
{
use LaraSearch;
/**
* Columns that are considered for search results.
*
* @var array
*/
protected $searchable = [
'first_name', 'last_name', 'email'
];
}
// search string
$queryString = 'xyz';
// Simple search
$users = User::search($queryString)->get();
// Search and get relations
// It will not get the relations if you don't do this
$users = User::search($queryString)
->with('posts')
->get();
// Search with relations and pagination
$users = User::search($queryString)
->with('posts')
->paginate(20);
// Search only active users check
$users = User::where('status', 'active')
->search($queryString)
->paginate(20);
// fulltext or like
'formula' => env('LARA_SEARCH_TYPE', 'fulltext'),
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
});
// Full Text Index
DB::statement('ALTER TABLE users ADD FULLTEXT fulltext_index (first_name, last_name, email)');
}