### Install Laravel Searchable Source: https://github.com/mozex/laravel-searchable/blob/main/README.md Install the package using Composer. Requires PHP 8.2+. ```bash composer require mozex/laravel-searchable ``` -------------------------------- ### Scoping Search in Livewire/Controllers Source: https://github.com/mozex/laravel-searchable/blob/main/resources/boost/skills/searchable-development/SKILL.md Apply search filters and ordering within Livewire components or controllers. This example demonstrates searching user projects and paginating results. ```php $results = auth()->user() ->projects() ->search($this->search, except: ['user.name', 'user.email']) ->orderByDesc('updated_at') ->paginate(); ``` -------------------------------- ### Run Package Commands Source: https://github.com/mozex/laravel-searchable/blob/main/CLAUDE.md Use these Composer scripts to execute linting, static analysis, and test suites. ```bash composer test # lint + phpstan + type-coverage + pest composer test:unit # pest only composer test:types # phpstan only composer test:lint # pint --test (check only) composer lint # pint (fix) ``` -------------------------------- ### Package Architecture Overview Source: https://github.com/mozex/laravel-searchable/blob/main/CLAUDE.md The directory structure of the core package components. ```text src/ Searchable.php # The trait (core feature) SearchableServiceProvider.php # Registers Filament macros conditionally Filament/ SearchableGlobalSearchProvider.php # Filament global search provider ``` -------------------------------- ### Apply Search with Parameters Source: https://github.com/mozex/laravel-searchable/blob/main/README.md Pass additional constraints like 'in' or 'except' to the applySearch method. ```php $query->getModel()->applySearch($query, 'term', in: ['title', 'body']); $query->getModel()->applySearch($query, 'term', except: ['author.name']); ``` -------------------------------- ### Register Global Search Provider in Filament PanelProvider Source: https://context7.com/mozex/laravel-searchable/llms.txt Register `SearchableGlobalSearchProvider` in your `PanelProvider` to enable advanced search across all resources in your Filament panel. Ensure each resource defines `getGloballySearchableAttributes()`. ```php id('admin') ->path('admin') ->globalSearch(SearchableGlobalSearchProvider::class) ->resources([ CourseResource::class, PostResource::class, UserResource::class, ]); } } ``` -------------------------------- ### Register Global Search Provider Source: https://github.com/mozex/laravel-searchable/blob/main/README.md Register the SearchableGlobalSearchProvider in your Filament panel configuration. ```php use Mozex Searchable Filament SearchableGlobalSearchProvider; return $panel ->id('admin') ->path('admin') ->globalSearch(SearchableGlobalSearchProvider::class); ``` -------------------------------- ### Invoke applySearch Method Source: https://context7.com/mozex/laravel-searchable/llms.txt Use applySearch to bypass query builder conflicts or integrate with third-party packages like Corcel. ```php getModel()->applySearch($query, 'laravel'); $results = $query->get(); // With column filtering parameters $query = Post::query(); $query->getModel()->applySearch($query, 'term', in: ['title', 'body']); $results = $query->get(); $query = Post::query(); $query->getModel()->applySearch($query, 'term', except: ['author.name']); $results = $query->get(); // Example: Corcel WordPress integration use Corcel\\Model\\Post as CorcelPost; use Mozex\\Searchable\\Searchable; class WordPressPost extends CorcelPost { use Searchable; public function searchableColumns(): array { return ['post_title', 'post_content']; } } // $query->search() would call Corcel's Builder method, so use applySearch instead $query = WordPressPost::query(); $query->getModel()->applySearch($query, 'wordpress'); $posts = $query->get(); // Alternative: Override the Builder's search method to delegate to applySearch class WordPressPostBuilder extends \\Corcel\\Model\\Builder\\PostBuilder { public function search($term = false, ...$args): self { $query = WordPressPost::query(); (new WordPressPost)->applySearch($query, $term, ...$args); return $query; } } ``` -------------------------------- ### Register Global Search Provider Source: https://github.com/mozex/laravel-searchable/blob/main/resources/boost/skills/searchable-development/SKILL.md Register the SearchableGlobalSearchProvider in your Filament panel configuration to enable global search functionality. ```php use Mozex\Searchable\Filament\SearchableGlobalSearchProvider; return $panel->globalSearch(SearchableGlobalSearchProvider::class); ``` -------------------------------- ### Execute Search Queries Source: https://context7.com/mozex/laravel-searchable/llms.txt Perform searches using the search() query scope, which can be chained with other Eloquent constraints or pagination. ```php get(); // Chain with other query constraints (search applies as AND condition) $publishedPosts = Post::query() ->where('published', true) ->search($request->input('q')) ->paginate(15); // Search with pagination $results = Comment::search('framework') ->orderBy('created_at', 'desc') ->paginate(20); // Empty/null search returns all records unfiltered $allPosts = Post::search(null)->get(); // Returns all posts $allPosts = Post::search('').get(); // Returns all posts ``` -------------------------------- ### Delegate Builder Search to applySearch Source: https://github.com/mozex/laravel-searchable/blob/main/resources/boost/skills/searchable-development/SKILL.md In custom Eloquent builders that own the `search()` method, delegate the call to `applySearch()` to maintain consistent usage across the codebase while leveraging the package's scope. ```php 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; } } ``` -------------------------------- ### Configure Relation Columns for Search Source: https://github.com/mozex/laravel-searchable/blob/main/README.md Use dot notation to search through BelongsTo, HasMany, and BelongsToMany relations. ```php public function searchableColumns(): array { return [ 'title', 'author.name', // BelongsTo 'author.email', // BelongsTo, different column 'comments.body', // HasMany 'tags.name', // BelongsToMany / HasMany ]; } ``` -------------------------------- ### Integrate Advanced Search in Filament Source: https://github.com/mozex/laravel-searchable/blob/main/README.md Use the advancedSearchable macro on Filament TextColumns to enable cross-column searching. ```php use Filament\Tables\Columns\TextColumn; TextColumn::make('title') ->advancedSearchable() ->sortable(); ``` ```php TextColumn::make('title') ->advancedSearchable(except: ['author.name']) ->sortable(); ``` -------------------------------- ### Apply Search Scope Manually Source: https://github.com/mozex/laravel-searchable/blob/main/resources/boost/skills/searchable-development/SKILL.md Use the `applySearch()` method to invoke the package's search scope directly on a query builder instance. This is useful when the `search()` method is unavailable or conflicts with other implementations. ```php $query = Product::query(); $query->getModel()->applySearch($query, 'term', in: ['title']); $results = $query->get(); ``` -------------------------------- ### Implement Searchable Columns with Morph Relations Source: https://github.com/mozex/laravel-searchable/blob/main/README.md Define searchable columns in the model, including support for nested relations through morph targets. ```php 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(); } } ``` -------------------------------- ### Implement Direct Column Search Source: https://context7.com/mozex/laravel-searchable/llms.txt Define searchable columns in the model to enable partial matching on specific database fields. ```php get(); // Partial matching works automatically Post::factory()->create(['title' => 'Introduction to Laravel']); $results = Post::search('Intro', in: ['title'])->get(); // Returns the post with "Introduction to Laravel" // Multiple direct columns with OR logic Post::factory()->create(['title' => 'Laravel', 'body' => 'PHP framework']); Post::factory()->create(['title' => 'React', 'body' => 'Laravel integration']); $results = Post::search('Laravel', in: ['title', 'body'])->get(); // Returns both posts (matches title OR body) ``` -------------------------------- ### Implement Relation Column Search Source: https://context7.com/mozex/laravel-searchable/llms.txt Use dot notation to search through BelongsTo, HasMany, and BelongsToMany relationships. ```php belongsTo(Author::class); } } // Find posts by author name $john = Author::factory()->create(['name' => 'John Doe']); Post::factory()->create(['author_id' => $john->id, 'title' => 'My Post']); $results = Post::search('John', in: ['author.name'])->get(); // Returns posts where author name contains "John" // Search HasMany relations (find authors by their post titles) class Author extends Model { use Searchable; public function searchableColumns(): array { return ['name', 'email', 'posts.title']; } public function posts(): HasMany { return $this->hasMany(Post::class); } } $authors = Author::search('Laravel Guide', in: ['posts.title'])->get(); // Returns authors who have posts with "Laravel Guide" in title ``` -------------------------------- ### Configure Direct Columns for Search Source: https://github.com/mozex/laravel-searchable/blob/main/README.md Specify direct columns on the model's table to be included in search results. ```php public function searchableColumns(): array { return ['title', 'body', 'slug']; } ``` -------------------------------- ### Configure Searchable Trait in Model Source: https://context7.com/mozex/laravel-searchable/llms.txt Implement the Searchable trait and define searchable columns, including support for relations and nested morph relations. ```php belongsTo(User::class); } public function commentable(): MorphTo { return $this->morphTo(); } } ``` -------------------------------- ### Delegate Search in Custom Builders Source: https://github.com/mozex/laravel-searchable/blob/main/README.md Override a custom Builder's search method to delegate to applySearch for consistent API usage. ```php 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; } } ``` -------------------------------- ### Basic Search Usage Source: https://github.com/mozex/laravel-searchable/blob/main/resources/boost/skills/searchable-development/SKILL.md Perform a search on an Eloquent model using the ->search() scope. This can be used as the shortest form or chained with other query constraints. ```php // Shortest form Comment::search('term')->get(); ``` ```php // Chain with other constraints Comment::query()->where('published', true)->search('term')->paginate(); ``` -------------------------------- ### Configure Cross-Database Relation Search Source: https://context7.com/mozex/laravel-searchable/llms.txt Define models with different database connections and searchable columns to enable automatic subquery-based searching. ```php belongsTo(Category::class); } } // Cross-database search works automatically $category = Category::factory()->create(['name' => 'Programming']); Post::factory()->create(['category_id' => $category->id, 'title' => 'My Post']); $results = Post::search('Programming', in: ['category.name'])->get(); // Runs subquery on external connection, then whereIn on post's foreign key // Returns posts in "Programming" category // External morph relation also supported class Comment extends Model { use Searchable; public function searchableColumns(): array { return [ 'body', 'commentable:category.name', // External morph (category on different DB) ]; } } $results = Comment::search('Web Development', in: ['commentable:category.name'])->get(); ``` -------------------------------- ### Define Globally Searchable Attributes in Filament Resource Source: https://context7.com/mozex/laravel-searchable/llms.txt Define `getGloballySearchableAttributes()` in your Filament resource to specify which columns should be included in the global search. You can either use all columns from the model's `searchableColumns()` or provide a subset. ```php class CourseResource extends Resource { protected static ?string $model = Course::class; // Use all columns from model's searchableColumns() public static function getGloballySearchableAttributes(): array { return (new Course)->searchableColumns(); } } ``` ```php class PostResource extends Resource { protected static ?string $model = Post::class; // Or limit to a subset for global search public static function getGloballySearchableAttributes(): array { return ['title', 'author.name']; // Only these columns in global search } } ``` -------------------------------- ### Define Globally Searchable Attributes Source: https://github.com/mozex/laravel-searchable/blob/main/README.md Implement getGloballySearchableAttributes in your Filament resources to specify which columns are searchable. ```php 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']; } } ``` -------------------------------- ### Perform Search Query Source: https://github.com/mozex/laravel-searchable/blob/main/README.md Use the search method on your Eloquent model to perform searches. It can be chained with other query constraints. ```php // 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(); ``` -------------------------------- ### Use Renamed Search Method in Filament Source: https://github.com/mozex/laravel-searchable/blob/main/README.md Pass the aliased method name to the advancedSearchable configuration in Filament. ```php TextColumn::make('name')->advancedSearchable(method: 'databaseSearch') ``` -------------------------------- ### Invoke Search via applySearch Source: https://github.com/mozex/laravel-searchable/blob/main/README.md Use applySearch to directly invoke the search scope when the search method is unavailable or conflicted. ```php $query = Product::query(); $query->getModel()->applySearch($query, 'term'); $results = $query->get(); ``` -------------------------------- ### Column Filtering with 'include' Parameter Source: https://github.com/mozex/laravel-searchable/blob/main/resources/boost/skills/searchable-development/SKILL.md Add columns to the search for a single query, in addition to those defined in searchableColumns(). Accepts a string or an array of column names. ```php Post::search('term', include: ['slug'])->get(); ``` -------------------------------- ### Enable Advanced Search on Filament TextColumn Source: https://context7.com/mozex/laravel-searchable/llms.txt Add `advancedSearchable()` to a TextColumn to enable searching across all model's searchable columns. You can also specify columns to include or exclude from the search. ```php columns([ TextColumn::make('title') ->advancedSearchable() // Enables search across all searchableColumns ->sortable(), TextColumn::make('author.name') ->sortable(), TextColumn::make('created_at') ->dateTime(), ]); } } ``` ```php TextColumn::make('title') ->advancedSearchable(except: ['author.name']) // Exclude author from search ->sortable(); ``` ```php TextColumn::make('title') ->advancedSearchable(in: ['title', 'body']) // Only search these columns ->sortable(); ``` ```php TextColumn::make('title') ->advancedSearchable(include: ['slug']) // Add slug to searchable columns ->sortable(); ``` ```php // For Scout coexistence, specify the aliased method name TextColumn::make('name') ->advancedSearchable(method: 'databaseSearch') ->sortable(); ``` -------------------------------- ### Implement Laravel Scout Coexistence Source: https://context7.com/mozex/laravel-searchable/llms.txt Use trait aliasing to rename the package's search scope, allowing it to function alongside Laravel Scout's search methods. ```php $this->name, 'description' => $this->description, ]; } } // Now both search methods are available with clear names $scoutResults = Lesson::search('laravel')->get(); // Scout (Algolia/Meilisearch) $dbResults = Lesson::databaseSearch('laravel')->get(); // This package (SQL) // Chain database search with other constraints $filtered = Lesson::query() ->where('published', true) ->databaseSearch($request->input('q')) ->paginate(); ``` -------------------------------- ### Dynamic Column Composition Source: https://github.com/mozex/laravel-searchable/blob/main/resources/boost/skills/searchable-development/SKILL.md Dynamically compose searchable columns by including related model attributes. This allows for searching across nested relationships. ```php public function searchableColumns(): array { return [ 'name', 'slug', ...collect((new Faq)->searchableColumns()) ->map(fn (string $column): string => 'faqs.' . $column) ->toArray(), ]; } ``` -------------------------------- ### Configure Morph Map for Polymorphic Relations Source: https://github.com/mozex/laravel-searchable/blob/main/README.md Define the morph map in a ServiceProvider to map aliases to model classes. ```php // In a ServiceProvider: Relation::morphMap([ 'post' => Post::class, 'video' => Video::class, ]); ``` -------------------------------- ### Adjusting Cross-Database Cap with 'externalLimit' Source: https://github.com/mozex/laravel-searchable/blob/main/resources/boost/skills/searchable-development/SKILL.md Override the default limit for external relation and morph searches per query. This parameter is ignored if no external columns are involved. ```php Post::search('term', externalLimit: 200)->get(); ``` ```php $model->applySearch($query, 'term', externalLimit: 200); ``` ```php TextColumn::make('title')->advancedSearchable(externalLimit: 200); ``` -------------------------------- ### Implement Searchable Trait and Define Columns Source: https://github.com/mozex/laravel-searchable/blob/main/resources/boost/skills/searchable-development/SKILL.md Add the Searchable trait to your Eloquent model and define the columns to be searched using the searchableColumns method. This method accepts direct columns, relation columns, and morph relations. ```php use Mozex\Searchable\Searchable; class Comment extends Model { use Searchable; public function searchableColumns(): array { return [ 'body', 'author.name', 'tags.name', 'commentable:post.title', 'commentable:video.name', ]; } } ``` -------------------------------- ### Filter Searchable Columns Source: https://context7.com/mozex/laravel-searchable/llms.txt Adjust the search scope dynamically using 'in', 'include', and 'except' parameters to refine which columns are queried. ```php get(); $results = Post::search('laravel', in: 'title')->get(); // string also works // 'include' - Add extra columns ON TOP of searchableColumns $results = Post::search('laravel', include: ['slug', 'meta_description'])->get(); // 'except' - Remove specific columns FROM searchableColumns $results = Post::search('laravel', except: ['author.name'])->get(); $results = Post::search('laravel', except: 'body')->get(); // Combine parameters $results = Post::search('laravel', include: ['slug'], except: ['body'] )->get(); // Override and extend simultaneously $results = Post::search('laravel', in: ['title', 'body'], include: ['author.email'], except: ['body'] )->get(); ``` -------------------------------- ### Define Globally Searchable Attributes Source: https://github.com/mozex/laravel-searchable/blob/main/resources/boost/skills/searchable-development/SKILL.md Specify which model attributes should be included in global search results. This can include all searchable columns or a subset of specific fields. ```php // Use all searchable columns public static function getGloballySearchableAttributes(): array { return (new Course)->searchableColumns(); } ``` ```php // Or a subset public static function getGloballySearchableAttributes(): array { return ['title', 'author.name']; } ``` -------------------------------- ### Filter Searchable Columns Per Query Source: https://github.com/mozex/laravel-searchable/blob/main/README.md Override or modify the default searchable columns using in, include, and except parameters. ```php // Search only specific columns (ignores searchableColumns) Post::search('term', in: ['title', 'body'])->get(); // Add extra columns on top of searchableColumns Post::search('term', include: ['slug'])->get(); // Exclude specific columns from searchableColumns Post::search('term', except: ['author.name'])->get(); // Combine them Post::search('term', include: ['slug'], except: ['body'])->get(); ``` -------------------------------- ### Implement Morph Relation Search Source: https://context7.com/mozex/laravel-searchable/llms.txt Use relation:morphType.column notation for polymorphic relations, ensuring the morph map is configured. ```php \\App\\Models\\Post::class, 'video' => \\App\\Models\\Video::class, 'category' => \\App\\Models\\Category::class, ]); // Then define searchable columns with morph notation 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(); } } // Find comments on posts containing "Laravel" $post = Post::factory()->create(['title' => 'Laravel Testing']); Comment::factory()->create([ 'commentable_type' => 'post', 'commentable_id' => $post->id, 'body' => 'Great article!', ]); $results = Comment::search('Laravel', in: ['commentable:post.title'])->get(); // Returns comments attached to posts with "Laravel" in title // Nested morph relation search $author = Author::factory()->create(['name' => 'John Doe']); $post = Post::factory()->create(['author_id' => $author->id]); Comment::factory()->create([ 'commentable_type' => 'post', 'commentable_id' => $post->id, ]); $results = Comment::search('John', in: ['commentable:post.author.name'])->get(); // Returns comments on posts written by authors named "John" ``` -------------------------------- ### Adjust External Database Search Limit Source: https://github.com/mozex/laravel-searchable/blob/main/README.md Override the default 50-row limit for cross-database relation searches using the externalLimit parameter. ```php Post::search('term', externalLimit: 200)->get(); ``` -------------------------------- ### Define Searchable Columns in Eloquent Model Source: https://github.com/mozex/laravel-searchable/blob/main/README.md Add the Searchable trait to your Eloquent model and define the searchableColumns method to specify which columns and relations to search. ```php 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(); } } ``` -------------------------------- ### Filament Table Column Macro with 'advancedSearchable' Source: https://github.com/mozex/laravel-searchable/blob/main/resources/boost/skills/searchable-development/SKILL.md Enable advanced searching on a Filament table column. This macro searches all columns configured in searchableColumns() and can be filtered. ```php TextColumn::make('title')->advancedSearchable()->sortable(), ``` ```php TextColumn::make('title')->advancedSearchable(except: ['author.name']), ``` ```php TextColumn::make('title')->advancedSearchable(method: 'databaseSearch'), ``` -------------------------------- ### Alias Search Scope for Scout Coexistence Source: https://github.com/mozex/laravel-searchable/blob/main/resources/boost/skills/searchable-development/SKILL.md Alias the package's scope method to avoid naming conflicts when using Laravel Scout's `search()` method on the same model. This allows distinct calls for Scout and the package's search. ```php use Laravel\Scout\Searchable; use Mozex\Searchable\Searchable as DatabaseSearchable; class Lesson extends Model { use DatabaseSearchable { scopeSearch as scopeDatabaseSearch; } use Searchable; } ``` -------------------------------- ### Column Filtering with 'in' Parameter Source: https://github.com/mozex/laravel-searchable/blob/main/resources/boost/skills/searchable-development/SKILL.md Limit the search to specific columns for a single query, ignoring the columns defined in searchableColumns(). Accepts a string or an array of column names. ```php Post::search('term', in: ['title', 'body'])->get(); ``` ```php Post::search('term', in: 'title')->get(); ``` -------------------------------- ### Column Filtering with 'except' Parameter Source: https://github.com/mozex/laravel-searchable/blob/main/resources/boost/skills/searchable-development/SKILL.md Remove specific columns from the search for a single query, even if they are defined in searchableColumns(). Accepts a string or an array of column names. ```php Post::search('term', except: ['author.name'])->get(); ``` -------------------------------- ### Resolve Search Method Conflicts with Trait Aliasing Source: https://github.com/mozex/laravel-searchable/blob/main/README.md Use PHP trait aliasing to rename the scopeSearch method when it conflicts with other packages like Laravel Scout. ```php 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']; } } ``` -------------------------------- ### Alias Scope for Parent Model Signature Conflict Source: https://github.com/mozex/laravel-searchable/blob/main/resources/boost/skills/searchable-development/SKILL.md When a parent model declares `scopeSearch` with a different signature, alias the trait's scope method to prevent fatal errors due to incompatible method signatures. ```php use Mozex\Searchable\Searchable as DatabaseSearchable; class Product extends VendorModel { use DatabaseSearchable { scopeSearch as scopeDatabaseSearch; } } ``` -------------------------------- ### Alias Scope in Inherited Models Source: https://github.com/mozex/laravel-searchable/blob/main/README.md Alias the scopeSearch method when extending a model that already defines a conflicting scopeSearch. ```php use Mozex Searchable Searchable as DatabaseSearchable; class Product extends VendorModel { use DatabaseSearchable { scopeSearch as scopeDatabaseSearch; } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.