This package makes it easy to search your Laravel models.
You can install the package via composer:
composer require craftcodery/laravel-searchable
In order to search through models you'll have to use the Searchable
trait and add the toSearchableArray
method.
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use CraftCodery\Searchable\Searchable;
class User extends Model
{
use Searchable;
/**
* Get the searchable data array for the model.
*
* @return array
*/
public function toSearchableArray()
{
return [
'columns' => [
'users.name' => 60,
'users.email' => 60,
'locations.city' => 40,
],
'joins' => [
'locations' => [
'users.location_id',
'locations.id'
],
],
'groupBy' => 'users.id'
];
}
}
To search your models, just use the search
method.
$users = User::search('john')->get();
You can configure the different search matchers and weights given to each used by the package.
php artisan vendor:publish --tag=searchable-config
The MIT License (MIT). Please see License File for more information.