1. Go to this page and download the library: Download netcore/module-search 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/ */
netcore / module-search example snippets
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' => [...]
]
]
];
}
...
}
$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);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.