Searching backend

This commit is contained in:
Peter Papp
2021-04-22 09:33:52 +02:00
parent 5b70066900
commit 73861f814c
8 changed files with 166 additions and 27 deletions
+25 -2
View File
@@ -6,10 +6,12 @@ use App\Models\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Laravel\Scout\Searchable;
use TeamTNT\TNTSearch\Indexer\TNTIndexer;
class Client extends Model
{
use HasFactory;
use HasFactory, Searchable;
public $guarded = ['id'];
@@ -27,12 +29,33 @@ class Client extends Model
return $this->hasMany(Invoice::class, 'client_id', 'id');
}
/**
* Index file
*
* @return array
*/
public function toSearchableArray()
{
$array = $this->toArray();
$client_name = Str::slug($array['name'], ' ');
$client_email = Str::slug($array['email'], ' ');
return [
'id' => $this->id,
'clientName' => $array['name'],
'clientNameNgrams' => utf8_encode((new TNTIndexer)->buildTrigrams(implode(', ', [$client_name]))),
'clientEmail' => $array['email'],
'clientEmailNgrams' => utf8_encode((new TNTIndexer)->buildTrigrams(implode(', ', [$client_email]))),
];
}
protected static function boot()
{
parent::boot();
static::creating(function ($order) {
$order->id = Str::uuid();
$order->id = (string) Str::uuid();
});
}
}