PHP code example of codewiser / laravel-scout

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

    

codewiser / laravel-scout example snippets


$users = User::search('query')
    ->where('created_at', '>', now()->subYear())
    ->orderBy('created_at');

$users = User::search('query', function () {
        // Customize search here
        // Function arguments differs depending on driver
    })
    ->orderBy('created_at');

use Codewiser\Scout\Concerns\Scout;
use Codewiser\Scout\Concerns\ScoutsDatabase;
use Codewiser\Scout\Concerns\ScoutsMeilisearch;
use Codewiser\Scout\Meilisearch\MeilisearchBuilder;
use Illuminate\Contracts\Database\Eloquent\Builder;

class UserScout extends Scout implements ScoutsDatabase, ScoutsMeilisearch
{
    public function __construct(public ?int $min_comments) {
        //
    }
    
    public function database(Builder $builder) : Builder
    {
        return $builder
            ->has('comments', '>=', $this->min_comments);
    }
    
    public function meilisearch(MeilisearchBuilder $builder) : MeilisearchBuilder
    {
        return $builder
            ->where('comments_count', '>=', $this->min_comments)
    }
}

public function index(\Illuminate\Http\Request $request)
{
    $scouter = new UserScout(min_comments: $request->input('filter.min_comments'));
    
    $users = User::search('query', $scouter)
        ->orderBy('created_at');
    
    // Debug search filters
    dump($scouter->debug());
    
    return $users->paginate();
}

use App\Models\User;
use App\Models\Flight;
 
'meilisearch' => [
    'host' => env('MEILISEARCH_HOST', 'http://localhost:7700'),
    'key' => env('MEILISEARCH_KEY', null),
    'index-settings' => [
        User::class => [
            'filterableAttributes'=> ['id', 'name', 'email'],
            'sortableAttributes' => ['created_at'],
            // Other settings fields...
        ],
        Flight::class => [
            'filterableAttributes'=> ['id', 'destination'],
            'sortableAttributes' => ['updated_at'],
        ],
    ],
],

use Codewiser\Scout\Attributes\MeilisearchFilterableAttributes;
use Codewiser\Scout\Attributes\MeilisearchSortableAttributes;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use Searchable;
    
    #[MeilisearchFilterableAttributes(['id', 'name', 'email'])]
    #[MeilisearchSortableAttributes(['created_at'])]
    public function toSearchableArray()
    {
        return $this->toArray();
    }
}

use App\Models\User;
 
'meilisearch' => [
    'host' => env('MEILISEARCH_HOST', 'http://localhost:7700'),
    'key' => env('MEILISEARCH_KEY', null),
    'searchable' => [
        User::class
    ],
],