webard/nova-eloquent-searchable is a Laravel package.
It currently has 1 GitHub stars and 2.747 downloads on Packagist (latest version 1.0.0).
Install it with composer require webard/nova-eloquent-searchable.
Discover more Laravel packages by webard
or browse all Laravel packages to compare alternatives.
Last updated
This simple package comes with two solutions:
composer require webard/nova-eloquent-searchable
public static $search or public static searchableColumns() definition from Nova Resource to Model.use Webard\NovaEloquentSearchable\Trait\NovaEloquentSearch;
class User extends Resource
{
use NovaEloquentSearch;
}
use Webard\NovaEloquentSearchable\Trait\EloquentSearchable;
class User extends Authenticatable
{
use EloquentSearchable;
}
Now, you can search your models and return values the same way like in Laravel Nova panel:
$user = User::searchInDatabase('[email protected]')->first();
dump($user->name);
To not conflict with Laravel Scout, scope was called searchInDatabase, but if you want use other name, e.g. search, just rename Trait method:
class User extends Authenticatable
{
use EloquentSearchable {
scopeSearchInDatabase as scopeSearch;
}
}
Now, you can search by search method:
$user = User::search('[email protected]')->first();
dump($user->name);
Laravel Nova comes with some Searchable Columns Classes, but they can search only via MySQL's LIKE or full-text search.
Full-text search is powerful, but "like" searches on large datasets are very inefficient and they cannot use indexes, so this package derives several additional classes along with value validation functionality to optimize searches.
EqualValueuse Webard\NovaEloquentSearchable\Query\Search\EqualValue;
public static function searchableColumns(): array
{
return [
'name',
(new EqualValue('email'))
];
}
EqualValue is used to search for full values. This is a simple comparison in MySQL WHERE column="value". This search method is useful, for example, for searching by e-mail address or telephone number.
Additionally, you can add value validation, so that if the passed value does not meet the condition, it will not be added to the search query.
use Webard\NovaEloquentSearchable\Query\Search\EqualValue;
public static function searchableColumns(): array
{
return [
'name',
(new EqualValue('email'))
->validate(fn($value) => Validator::make(
[
'value' => $value
],
[
'value' => 'email'
]
)
->passes()
),
];
}
EqualRelationEqualRelation class do the same thing as EqualValue, but basing on relation instead of column.
Example:
use Webard\NovaEloquentSearchable\Query\Search\EqualValue;
public static function searchableColumns(): array
{
return [
'name',
(new EqualRelation('additionalEmails', 'email'))
->validate(fn($value) => Validator::make(
[
'value' => $value
],
[
'value' => 'email'
]
)
->passes()
),
];
}
FullTextValueFullTextValue do the same thing as SearchableText class from Laravel Nova, but additionally adds double quote (") characters to search value. So if you search for John Doe, to SQL query there is sent "John Doe" value. This way, searching in full-text mode is more precise.
Of course value validation like in EqualValue is present too.
FullTextRelationAs above, but works with relations instead of columns.
Of course value validation like in EqualValue is present too.
I'm are actively seeking contributions to enhance this package. Here are some features I would love to see implemented:
validate method to Laravel Nova searchable classesWe welcome contributions to improve this plugin! Please follow these steps to contribute:
This project is licensed under the MIT License. See the LICENSE.md file for more details.
For questions or support, please open an issue on GitHub.