PHP code example of sebastiansulinski / laravel-search

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

    

sebastiansulinski / laravel-search example snippets


'models' => [
    App\Models\Book::class,
    App\Models\Movie::class,
]

'typesense' => [
    'api_key' => env('TYPESENSE_API_KEY', 'xyz'),
    'nodes' => [
        [
            'host' => env('TYPESENSE_HOST', 'localhost'),
            'port' => env('TYPESENSE_PORT', '8108'),
            'path' => env('TYPESENSE_PATH', ''),
            'protocol' => env('TYPESENSE_PROTOCOL', 'http'),
        ],
    ],
    'nearest_node' => [
        'host' => env('TYPESENSE_HOST', 'localhost'),
        'port' => env('TYPESENSE_PORT', '8108'),
        'path' => env('TYPESENSE_PATH', ''),
        'protocol' => env('TYPESENSE_PROTOCOL', 'http'),
    ],
    'connection_timeout_seconds' => env('TYPESENSE_CONNECTION_TIMEOUT_SECONDS', 2),
    'healthcheck_interval_seconds' => env('TYPESENSE_HEALTHCHECK_INTERVAL_SECONDS', 30),
    'num_retries' => env('TYPESENSE_NUM_RETRIES', 3),
    'retry_interval_seconds' => env('TYPESENSE_RETRY_INTERVAL_SECONDS', 1),
],

use Illuminate\Database\Eloquent\Model;
use SebastianSulinski\Search\IndexableDocument;
use SebastianSulinski\Search\SearchIndexable;

class Book extends Model implements IndexableDocument
{
    // ...
    use SearchIndexable;

public static function searchableAs(): array
{
    return ['global_search'];
}

public function toSearchableArray(): array
{
    return [
        'global_search' => [
            'type' => 'book',
            'id' => $this->getSearchKey(),
            'name' => $this->name,
            'author' => $this->author,
            'description' => $this->description,
            'created_at' => $this->created_at->timestamp,
        ],
    ];
}

\SebastianSulinski\Search\Facades\Search::validation('global_search', fn (\Illuminate\Foundation\Http\FormRequest $request) => [
    'params.q' => [
        'nullable',
        'string',
    ],
    'params.query_by' => [
        '    'string',
    ],
    'params.page' => [
        'nullable',
        'integer',
    ],
    'params.per_page' => [
        'nullable',
        'integer',
    ],
    'params.sort_by' => [
        'nullable',
        'string',
    ],
]);

\SebastianSulinski\Search\Facades\Search::withoutRoutes();
bash
php artisan vendor:publish --provider="SebastianSulinski\Search\SearchServiceProvider"
bash
php artisan app:purge-search global_search