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
+24 -2
View File
@@ -7,10 +7,12 @@ use Carbon\Carbon;
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 Invoice extends Model
{
use HasFactory;
use HasFactory, Searchable;
protected $casts = [
'items' => 'array',
@@ -34,12 +36,32 @@ class Invoice extends Model
return $this->hasOne(Client::class, 'id', 'user_id');
}
/**
* Index file
*
* @return array
*/
public function toSearchableArray()
{
$array = $this->toArray();
$client_name = Str::slug($array['client']['name'], ' ');
return [
'id' => $this->id,
'clientName' => $array['client']['name'],
'clientNameNgrams' => utf8_encode((new TNTIndexer)->buildTrigrams(implode(', ', [$client_name]))),
'invoiceNumber' => $array['invoice_number'],
'invoiceNumberNgrams' => utf8_encode((new TNTIndexer)->buildTrigrams(implode(', ', [$array['invoice_number']]))),
];
}
protected static function boot()
{
parent::boot();
static::creating(function ($invoice) {
$invoice->id = Str::uuid();
$invoice->id = (string)Str::uuid();
$invoice->delivery_at = $invoice->created_at;
$invoice->due_at = Carbon::parse($invoice->created_at)->addWeeks(2);