1. Go to this page and download the library: Download mozex/laravel-searchable 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/ */
mozex / laravel-searchable example snippets
use Mozex\Searchable\Searchable;
class Comment extends Model
{
use Searchable;
public function searchableColumns(): array
{
return [
'body', // direct column
'author.name', // BelongsTo relation
'tags.name', // HasMany relation
'commentable:post.title', // morph relation
'commentable:video.name', // another morph type
];
}
public function author(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function commentable(): MorphTo
{
return $this->morphTo();
}
}
// Shortest form, searches all configured columns
Comment::search('laravel')->get();
// Chain with other query constraints
Comment::query()
->where('published', true)
->search($request->input('q'))
->paginate();
public function searchableColumns(): array
{
return ['title', 'body', 'slug'];
}
public function searchableColumns(): array
{
return [
'title',
'author.name', // BelongsTo
'author.email', // BelongsTo, different column
'comments.body', // HasMany
'tags.name', // BelongsToMany / HasMany
];
}
public function searchableColumns(): array
{
return [
'title',
'author.company.name', // Post -> Author -> Company
'comments.author.team.name', // three hops, still fine
];
}
// In a ServiceProvider:
Relation::morphMap([
'post' => Post::class,
'video' => Video::class,
]);
class Comment extends Model
{
use Searchable;
public function searchableColumns(): array
{
return [
'body',
'commentable:post.title', // search Post's title
'commentable:video.name', // search Video's name
'commentable:post.author.name', // nested: Post -> Author -> name
];
}
public function commentable(): MorphTo
{
return $this->morphTo();
}
}
Post::search('term', externalLimit: 200)->get();
// Finds the author named "Jane Doe"
Author::search('Doe Jane')->get();
// "Jane" matches the name, "acme.com" matches the email
Author::search('Jane [email protected]')->get();
// Matches "Jane Doe", not "Doe, Jane"
Author::search('"Jane Doe"')->get();
// Quoted phrase plus a loose term
Author::search('"Jane Doe" senior')->get();
// Search only specific columns (ignores searchableColumns)
Post::search('term', in: ['title', 'body'])->get();
// Add extra columns on top of searchableColumns
Post::search('term', get();
use Mozex\Searchable\Filament\RelevanceSort;
RelevanceSort::$enabled = false;
// $search and $sortColumn come from your table's livewire component
RelevanceSort::apply($query, $search, $sortColumn);
use Mozex\Searchable\Filament\SearchableGlobalSearchProvider;
return $panel
->id('admin')
->path('admin')
->globalSearch(SearchableGlobalSearchProvider::class);
class CourseResource extends Resource
{
// Use everything the model declared as searchable
public static function getGloballySearchableAttributes(): array
{
return (new Course)->searchableColumns();
}
}
class PostResource extends Resource
{
// Or limit global search to a subset, even though
// the Post model has more columns in searchableColumns()
public static function getGloballySearchableAttributes(): array
{
return ['title', 'author.name'];
}
}
use Laravel\Scout\Searchable;
use Mozex\Searchable\Searchable as DatabaseSearchable;
class Lesson extends Model
{
use DatabaseSearchable {
scopeSearch as scopeDatabaseSearch;
}
use Searchable;
public function searchableColumns(): array
{
return ['name', 'description'];
}
}
use Mozex\Searchable\Searchable as DatabaseSearchable;
class Product extends VendorModel
{
use DatabaseSearchable {
scopeSearch as scopeDatabaseSearch;
}
}
class ProductBuilder extends \Corcel\Model\Builder\PostBuilder
{
public function search($term = false, ...$args): self
{
$query = Product::query();
(new Product)->applySearch($query, $term, ...$args);
return $query;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.