PHP code example of jamal / universal-search-filter

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

    

jamal / universal-search-filter example snippets


use Illuminate\Database\Eloquent\Model;
use Jamal\UniversalSearchFilter\Traits\UniversalSearchable;

class User extends Model
{
    use UniversalSearchable;

    // Optional per-model config
    protected array $universalSearch = [
        'columns' => ['name', 'email', 'phone'],
        'relations' => [
            'posts' => ['title', 'body'],
        ],
        'weights' => [
            'name' => 3,
            'email' => 2,
            'posts' => 1, // weight applied to each related column
        ],
        'fuzzy' => true,
        'exclude_zero_relevancy' => true
    ];
}

$search = request('q');

$users = User::query()
    ->universalSearch($search) // or ->whereUniversalSearch($search)
    ->paginate(15);

protected array $universalSearch = [
        'columns' => [
            'name',
            'email',
            'status'
        ],
        'weights' => [
            'name' => 5,   // highest importance
            'email' => 3,
            'status' => 1, // least important
        ],
];

DB::table('users')
    ->whereUniversalSearchOn($search, ['name', 'email', 'phone'])
    ->paginate(20);

'use_scout_when_available' => true,

$users = User::universalSearch('"john doe"')
    ->with('posts')
    ->paginate();

$users = User::universalSearch($search, [
    'fuzzy' => false, // switch to prefix matching
    'min_token_length' => 3,
]);

$orders = Order::query()
    ->whereUniversalSearch($search, ['columns' => ['reference', 'status']])
    ->paginate();