1. Go to this page and download the library: Download ctf0/easy-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/ */
ctf0 / easy-searchable example snippets
use ctf0\EasySearchable\Traits\HasSearch;
class Post extends Model
{
use HasSearch;
// searched attributes
//
// when empty, all model fields will be searched
// except "dates, primary_key, hidden"
public $searchableAttributes = [];
// or have a complete freedom and directly use
public function getSearchableAttributes()
{
return [
'name->' . app()->getLocale(),
// ...
];
}
// ignore attributes
//
// so instead of listing all the needed columns in `$searchableAttributes`
// you can ignore the ones you don't need here
public $searchableAttributesIgnore = [];
// searched relations
//
// when empty, no relation will be searched
// * under each relation add the '$searchableAttributes' and we will pick them up automatically
// * doesn't support nested relations
public $searchableRelations = [];
// we search using the full sentence,
// however if you prefer to search by words, then use
public $replaceSpace = true;
// to force searching dates
public $searchableDates = true;
}
// auto search in model & relations attributes
Post::search('search for something')->get();
// to strict search even further, wrap the text with either `'` or `"`
Post::search('"search for something"')->get();
// search in specific fields
Post::search('search for something', ['columnName','relation.columnName'])->get();
// model
public function scopeWorkerSearch($query, $searchTerm)
{
return $query->orWhereHasMorph(
'workerable', // name of the morph relation
'*',
function ($q) use ($searchTerm) {
$q->search($searchTerm);
}
);
}
// controller
return $query->search($text)->workerSearch($text);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.