netcore / module-search
Search module.
Installs: 178
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 6
Forks: 0
Open Issues: 0
Type:module
Requires
- php: >=7.0
This package is not auto-updated.
Last update: 2024-11-10 05:14:21 UTC
README
This module was made for easy searching in database.
Features
- Multiple models per request
- Search through multi-level relations
Pre-installation
This module is part of Netcore CMS ecosystem and is only functional in a project that has following packages installed:
Installation
- Require this package using composer
composer require netcore/module-search
- Add SearchableContract and configure searchable models
use Illuminate\Database\Eloquent\Model; use Modules\Search\Contracts\SearchableContract; class Article extends Model implements SearchableContract { ... /** * Get the config of search module for building queries. * * @return array */ public function getSearchConfig(): array { return [ // Base table colums 'columns' => [ 'slug', 'author_name', 'author_surname', ], // Add constant wheres if necessary // Helpful for columns like is_active, is_published etc.. 'wheres' => [ 'is_active' => true, ], // Search in relations 'relations' => [ 'translations' => [ 'columns' => [ 'title', 'content', 'intro_text', ], 'wheres' => [ 'locale' => app()->getLocale() // search only in translations for current locale ], // Nested relations supported 'relations' => [...] ] ] ]; } ... }
Searching records
- To find something, you must use search() helper method
$page = (int)request('page', 1) ?? 1; $results = search() ->of(\App\Article::class) // set searchable models (multiple allowed) ->setPage($page) // optional - set the current page (1st is by default) ->setRecordsPerPage(15) // optional - set records count per page (20 is by default) ->withLoggedQueries() // optional - enables query logging ->returnAsCollection() // optional - returns collection instead of array ->where(\App\Article::class, 'translations.is_active', true) // add additional where to translations relation ->where(\App\Article::class, 'is_awesome', true) // add additional where to base model ->find('keyword'); dd($results);