1. Go to this page and download the library: Download eelcol/laravel-meilisearch 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/ */
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class Product extends Model
{
use HasFactory;
protected $guarded = ['id'];
public function toMeilisearch(): array
{
return [
'id' => $this->id,
'title' => $this->title,
'slug' => Str::slug($this->title),
];
}
}
$product = App\Models\Product::find(1);
Meilisearch::addDocument('products', $product);
// the product will be inserted like:
// [
// 'id' => 1,
// 'title' => 'iPhone SE',
// 'slug' => 'iphone-se'
// ]
// this query will return both iPhone SE and Samsung Galaxy
MeilisearchQuery::index('products')
->whereMatches('categories', ['phones', 'smartphones'])
->get();
// this query will return ONLY the iPhone SE
MeilisearchQuery::index('products')
->whereMatches('categories', ['phones', 'iphone'])
->get();
// this query will return ONLY the Samsung Galaxy
MeilisearchQuery::index('products')
->whereMatches('categories', ['phones', 'samsung'])
->get();