File: //home/armgroup/libs/app/Http/Livewire/Frontend/Search/Index.php
<?php
namespace App\Http\Livewire\Frontend\Search;
use App\Http\Livewire\ConvertEmptyStringsToNull;
use App\Models\Attribute;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Option;
use App\Models\Product;
use App\Models\Tag;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
use Livewire\WithPagination;
class Index extends Component
{
use ConvertEmptyStringsToNull, WithPagination;
protected $paginationTheme = 'bootstrap';
protected $queryString = ['q', 'special'];
public function updatingSort()
{
$this->resetPage();
$this->emit('scrollTop');
}
public function updatingSpecial()
{
$this->resetPage();
$this->emit('scrollTop');
}
public function updatingPhrase()
{
$this->resetPage();
$this->emit('scrollTop');
}
public function updatingBrandId()
{
$this->resetPage();
$this->emit('scrollTop');
}
public function updatingStock()
{
$this->resetPage();
$this->emit('scrollTop');
}
public function updatingMinPrice()
{
$this->resetPage();
$this->emit('scrollTop');
}
public function updatingMaxPrice()
{
$this->resetPage();
$this->emit('scrollTop');
}
public function updatingValueId()
{
$this->resetPage();
$this->emit('scrollTop');
}
public Category $category;
public Brand $brand;
public Tag $tag;
public $sort = 'latest';
public $phrase;
public $categories = [];
public $stock = 0;
public $min_price = 0;
public $faq;
public $tags;
public $max_price = null;
public $max_price_products;
public $attributes = [];
public $value_id = [];
public $special;
public $brand_id = [];
public $brands = [];
public $q;
public $show_filters = false;
public $options = [];
public $option_id = [];
public function mount()
{
$this->tags = Tag::query()->inRandomOrder()->limit(10)->get(['id', 'name', 'slug']);
$this->categories = Category::query()
->where('status', true)
->where('type', Product::class)
->when($this->category->id, function ($query) {
return $query->where('parent_id', $this->category->id);
})->when(! $this->category->id, function ($query) {
return $query->whereNull('parent_id');
})->get();
if ($this->category->id) {
$this->show_filters = $this->category->activeChildren()->count() ? false : true;
}
}
public function render()
{
if (! in_array($this->sort, ['latest', 'oldest', 'minPrice', 'maxPrice'])) {
$this->sort = 'latest';
}
$products = Product::query()
->with(['options', 'activeOptionValues'])
->where('status', true)
->when($this->category->id, function ($query) {
return $query->whereHas('categories', function (Builder $query) {
$ids = $this->category->getAllActiveChildren()->pluck('id')->toArray();
array_push($ids, $this->category->id);
$query->whereIn('id', $ids);
});
})->when($this->brand->id, function ($query) {
return $query->where('brand_id', $this->brand->id);
})->when($this->tag->id, function ($query) {
return $query->whereRelation('tags', 'id', $this->tag->id);
});
$this->max_price_products = $products->max('price');
if ($this->show_filters) {
$attributes_ids = array_unique(DB::table('attribute_product')
->whereIn('product_id', $products->pluck('id')->toArray())
->pluck('attribute_id')
->toArray());
$this->attributes = Attribute::whereIn('id', $attributes_ids)->with('values')->get();
$options_ids = array_unique(DB::table('option_product')
->whereIn('product_id', $products->pluck('id')->toArray())
->pluck('option_id')
->toArray());
$this->options = Option::whereIn('id', $options_ids)->with('values')->get();
}
if (! $this->brand->id) {
$this->brands = Brand::whereIn('id', $products->pluck('brand_id')->toArray())->select('id', 'name',
'image')->get();
}
$products = $products->when($this->phrase, function ($query) {
return $query->where('name', 'LIKE', '%'.$this->phrase.'%');
})->when($this->q, function ($query) {
return $query->where(function ($qu) {
return $qu->where('name', 'LIKE', '%'.$this->q.'%')->orWhere('code', $this->q);
});
})->when($this->special, function ($query) {
return $query->where('stock', '>', 0)
->where('products.special', '>', 0)
->where('products.special_started_at', '<=', Carbon::now())
->where('products.special_ended_at', '>=', Carbon::now());
})->when($this->stock, function ($query) {
return $query->where('stock', '>', 0);
})->when($this->brand_id, function ($query) {
return $query->whereIn('brand_id', $this->brand_id);
})->when($this->min_price, function ($query) {
return $query->where('price', '>=', $this->min_price)
->where('stock', '>', 0);
})->when($this->max_price, function ($query) {
return $query->where('price', '<=', $this->max_price)
->where('stock', '>', 0);
})->when($this->value_id, function ($query) {
return $query->whereHas('values', function (Builder $q) {
$q->whereIn('id', $this->value_id);
});
})->when($this->option_id, function ($query) {
return $query->whereHas('optionValues', function (Builder $q) {
$q->whereIn('id', $this->option_id);
});
})->select(['id', 'name', 'latin_name', 'slug', 'image', 'price', 'stock', 'special', 'special_started_at', 'special_ended_at', 'country_code', 'code'])
->{$this->sort}();
return view('livewire.frontend.search.index', [
'products' => $products->paginate(32),
]);
}
public function previousPage()
{
$this->setPage(max($this->page - 1, 1));
$this->emit('scrollTop');
}
public function nextPage()
{
$this->setPage($this->page + 1);
$this->emit('scrollTop');
}
public function gotoPage($page)
{
$this->setPage($page);
$this->emit('scrollTop');
}
public function hydrate()
{
$this->emit('applyLozad');
}
}