add new search engine

This commit is contained in:
ludo
2024-01-31 23:45:58 +01:00
parent 79af996c63
commit 5c20e6d5d0
5 changed files with 59 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ namespace App\Models\Shop;
use App\Models\Botanic\Specie;
use App\Models\Botanic\Variety;
use App\Repositories\Shop\Articles;
use App\Traits\Model\HasComments;
use App\Traits\Model\Imageable;
use Fico7489\Laravel\EloquentJoin\Traits\EloquentJoin;
@@ -15,6 +16,7 @@ use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kirschbaum\PowerJoins\PowerJoins;
use Laravel\Scout\Searchable;
use Rinvex\Categories\Traits\Categorizable;
use Rinvex\Tags\Traits\Taggable;
use Spatie\MediaLibrary\HasMedia;
@@ -31,6 +33,7 @@ class Article extends Model implements HasMedia
use Imageable;
use Powerjoins;
use RevisionableTrait;
use Searchable;
use SoftDeletes;
use Taggable;
use UserStamps;
@@ -192,7 +195,31 @@ class Article extends Model implements HasMedia
public function scopeWithAvailableOffers($query, $saleChannelId = false)
{
return $query->whereHas('offers', function ($query) use ($saleChannelId) {
$query->active()->byStockAvailable()->bySaleChannel($saleChannelId);
$query->active()->byStockAvailable();
if ($saleChannelId) {
$query->bySaleChannel($saleChannelId);
}
});
}
public function toSearchableArray(): array
{
$description = $this->description;
$product = $this->product ?? false;
if (is_object($product)) {
$description .= ' ' . $product ? $product->description : '';
$description .= ' ' . $product ? $product->plus ?? '' : '';
$specie = $product ? $product->specie : false;
$description .= ' ' . $specie ? $specie->description ?? '' : '';
}
return [
'id' => (int) $this->id,
'name' => $this->name,
'description' => html_entity_decode(strip_tags($description)),
];
}
}

View File

@@ -2,8 +2,14 @@
namespace App\Repositories\Shop;
use App\Models\Shop\Article;
class Searches
{
public static function search($query)
{
return Article::withAvailableOffers()->search($query)->get();
}
public static function getResults($options)
{
return Articles::getArticlesToSell($options);