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/ */
$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 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();
}
}