PHP code example of fab2s / searchable

1. Go to this page and download the library: Download fab2s/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/ */

    

fab2s / searchable example snippets


use fab2s\Searchable\SearchableInterface;
use fab2s\Searchable\Traits\Searchable;

class Contact extends Model implements SearchableInterface
{
    use Searchable;

    protected $searchables = [
        'first_name',
        'last_name',
        'email',
    ];
}

$results = Contact::search($request->input('q'))->get();

$results = Contact::search('john')
    ->where('active', true)
    ->limit(10)
    ->get();

$results = Contact::search('john', null)->latest()->get();

> /**
>  * @method static Builder<static> search(string|array $search, ?string $order = 'DESC')
>  */
> class Contact extends Model implements SearchableInterface
> 

// Safe — returns all contacts (unfiltered) when $q is empty
$results = Contact::search($request->input('q', ''))
    ->where('active', true)
    ->get();

use fab2s\Searchable\SearchQuery;

$search = new SearchQuery('DESC', 'searchable', 'english', phonetic: true);
$query  = Contact::query();

$search->addMatch($query, $request->input('q'), 'contacts');

$results = $query->get();

$search = new SearchQuery;
$query  = Contact::query()
    ->join('companies', 'contacts.company_id', '=', 'companies.id')
    ->select('contacts.*');

// search in contacts
$search->addMatch($query, $request->input('q'), 'contacts');

// you could also search in companies with a second SearchQuery instance
// (new SearchQuery)->addMatch($query, $request->input('q'), 'companies');

$results = $query->get();

class Contact extends Model implements SearchableInterface
{
    use Searchable;

    protected array $searchables = ['first_name', 'last_name', 'email'];
    protected string $searchableTsConfig = 'french';
    protected bool $searchablePhonetic = true;
    protected int $searchableFieldDbSize = 1000;
}

public function getSearchableContent(string $additional = ''): string
{
    $extra = implode(' ', [
        $this->decrypt('phone'),
        $this->some_computed_value,
    ]);

    return parent::getSearchableContent($extra);
}

protected string $searchableTsConfig = 'french';

$search = new SearchQuery('DESC', 'searchable', 'french');

protected bool $searchablePhonetic = true;

$search = new SearchQuery('DESC', 'searchable', 'english', phonetic: true);

use fab2s\Searchable\Phonetic\PhoneticInterface;

class MyEncoder implements PhoneticInterface
{
    public static function encode(string $word): string
    {
        // your encoding logic
    }
}

use fab2s\Searchable\Phonetic\Phonetic;

class Contact extends Model implements SearchableInterface
{
    use Searchable;

    protected array $searchables = ['first_name', 'last_name'];
    protected bool $searchablePhonetic = true;
    protected string $searchablePhoneticAlgorithm = Phonetic::class;
}

$search = new SearchQuery('DESC', 'searchable', 'french', phonetic: true, phoneticAlgorithm: Phonetic::encode(...));

use fab2s\Searchable\Phonetic\Phonetic;
use fab2s\Searchable\Phonetic\Soundex2;

Phonetic::encode('jean');   // 'JAN'
Soundex2::encode('dupont'); // 'DIPN'
shell
php artisan searchable:enable
shell
php artisan searchable:enable --model=App/Models/Contact --index