PHP code example of chr15k / laravel-meilisearch-advanced-query

1. Go to this page and download the library: Download chr15k/laravel-meilisearch-advanced-query 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/ */

    

chr15k / laravel-meilisearch-advanced-query example snippets



use App\Models\User;
use Chr15k\MeilisearchAdvancedQuery\MeilisearchQuery;

$builder = MeilisearchQuery::for(User::class)
    ->where('name', 'Chris')
    ->whereIn('email', ['[email protected]', '[email protected]'])
    ->orWhere(fn ($query) => $query
        ->whereTo('login_count', 50, 400)
        ->orWhereIsEmpty('verified_at')
    )->sort(['name:desc', 'email:asc'])
     ->search($term); // returns Scout Builder instance

// continue to chain Scout methods
$results = $builder->paginate();

MeilisearchQuery::for(User::class)->where('name', 'Chris');

// "name = 'Chris'"

MeilisearchQuery::for(User::class)->orWhere('name', 'Chris')

// "name = 'Chris'"

MeilisearchQuery::for(User::class)
    ->where('name', 'Bob')
    ->orWhere('name', 'Chris')

// "name = 'Bob' OR name = 'Chris'"

MeilisearchQuery::for(User::class)
    ->whereIn('name', ['Chris', 'Bob']);

// "name IN ['Chris','Bob']"

MeilisearchQuery::for(User::class)
    ->orWhereIn('name', ['Chris', 'Bob']);

// "name IN ['Chris','Bob']"

MeilisearchQuery::for(User::class)
    ->where('email', '[email protected]')
    ->orWhereIn('name', ['Chris', 'Bob']) ;

// "email = '[email protected]' OR name IN ['Chris','Bob']"

MeilisearchQuery::for(User::class)
    ->whereNotIn('name', ['Chris', 'Bob']);

// "name NOT IN ['Chris','Bob']"

MeilisearchQuery::for(User::class)
    ->where('email', '[email protected]')
    ->orWhereNotIn('name', ['Chris', 'Bob']);

// "email = '[email protected]' OR name NOT IN ['Chris','Bob']"

MeilisearchQuery::for(User::class)
    ->whereNot('name', 'Chris');

// "NOT name 'Chris'"

MeilisearchQuery::for(User::class)
    ->where('email', '[email protected]')
    ->orWhereNot('name', 'Chris');

// "email = '[email protected]' OR NOT name 'Chris'"

MeilisearchQuery::for(User::class)->whereIsEmpty('name');

// "name IS EMPTY"

MeilisearchQuery::for(User::class)
    ->whereNot('name', 'Chris')
    ->orWhereIsEmpty('name');

// "NOT name 'Chris' OR name IS EMPTY"

MeilisearchQuery::for(User::class)->whereTo('count', 1, 10);

// "count 1 TO 10"

MeilisearchQuery::for(User::class)
    ->where('email', '[email protected]')
    ->orWhereTo('count', 1, 10);

// "email = '[email protected]' OR count 1 TO 10"

MeilisearchQuery::for(User::class)
    ->whereRaw("name = 'Chris' OR name = 'Bob'")
    ->compile();

// "name = 'Chris' OR name = 'Bob'"

MeilisearchQuery::for(User::class)
    ->whereRaw("name = 'Chris'")
    ->orWhereRaw("name = 'Bob'")
    ->compile();

// "name = 'Chris' OR name = 'Bob'"

MeilisearchQuery::for(User::class)
    ->where(fn ($query) => $query
        ->whereNot('name', 'Chris')
        ->orWhereIsEmpty('name')
    )
    ->orWhere('email', '[email protected]');

// "(NOT name 'Chris' OR name IS EMPTY) OR email = '[email protected]'"

MeilisearchQuery::for(User::class)
    ->where('name', 'Chris')
    ->orWhere('name', 'Bob')
    ->sort('name:desc');

MeilisearchQuery::for(User::class)
    ->where('name', 'Chris')
    ->orWhere('name', 'Bob')
    ->sort(['name:desc', 'email:asc']);

MeilisearchQuery::for(User::class)->where('name', 'NOT', 'Chris'); // "NOT name 'Chris'"
MeilisearchQuery::for(User::class)->where('count', 'TO', [1, 10]); // "count 1 TO 10"
MeilisearchQuery::for(User::class)->where('name', 'IS EMPTY'); // "name IS EMPTY"

MeilisearchQuery::for(User::class)->where('name', 'Chris'); // "name = 'Chris'"

MeilisearchQuery::for(User::class)
    ->where(fn ($query) => $query
        ->whereIn('name', ['Chris', 'Bob'])
        ->orWhereIsEmpty('verified_at')
    )
    ->orWhere('email', '[email protected]')
    ->compile();

// "(name IN ['Chris','Bob'] OR verified_at IS EMPTY) OR email = '[email protected]'"

MeilisearchQuery::for(User::class)->where('name', 'Chris')->inspect();

MeilisearchQuery::for(User::class)->where('name', 'Chris')->dump();