Jan 18, 2022
0
Level 4
Hi guys i face a problem i want when i selected categories appear all sub categories that belong on this categories.
I try with updated method and work but if i click on first categories the second was dissappear as follow and if click second third dissappear.
My livewire component
use WithPagination;
protected $paginationTheme = 'bootstrap';
public $SelectedCat = [];
public $subcategories= [];
public $SelectedSubCat = [];
public $SelectedTag = [];
public $search="";
public $location="";
public $filter = "";
public $min_price ="";
public $max_price ="";
protected $queryString = [
'SelectedCat' => ['except'=> ''],
'SelectedTag' => ['except'=> ''],
'SelectedSubCat'=>['except'=> ''],
'location'=>['except'=> ''],
'min_price' => ['except'=> ''],
'max_price' => ['except'=> ''],
'filter' => ['except'=> ''],
'search' =>['except'=> ''],
'subcategories' =>['except'=> ''],
];
public function updatingFilter()
{
$this->resetPage();
}
public function updatingMin_price()
{
$this->resetPage();
}
public function updatingMax_price()
{
$this->resetPage();
}
public function updatingLocation()
{
$this->resetPage();
}
public function updatedSelectedCat($value)
{
if($value){
$this->subcategories = Subcategory::whereHas('subcategoriesProductCategories',function($query){
return $query ->whereIn('product_category_id', array_keys($this->SelectedCat));
})->get()->toArray();
}else{
$this->subcategories =[];
}
}
public function render()
{
$categories =ProductCategory::all();
$tags = ProductTag::all();
$count_items = Product::count();
$items = Product::with(['categories'])
->when($this->SelectedCat = array_filter($this->SelectedCat),function($query)
{
return $query->whereHas('categories', function($query) {
return $query->whereIn('product_category_id', array_keys($this->SelectedCat));
});
})
->when($this->SelectedSubCat = array_filter($this->SelectedSubCat),function($query)
{
return $query->whereHas('subcategories', function($query) {
return $query->whereIn('subcategory_id', array_keys($this->SelectedSubCat));
});
})
->when($this->SelectedTag = array_filter($this->SelectedTag),function($query)
{
return $query->whereHas('tags', function($query) {
return $query->whereIn('product_tag_id', array_keys($this->SelectedTag));
});
})
->when(strlen($this->search) >= 3, function ($query) {
return $query->where('name', 'like', '%'.$this->search.'%');
})
->when(strlen($this->location) >=5 , function ($query) {
return $query->where('address_address', '=', $this->location);
})
->when($this->filter && $this->filter === 'Cheapest', function ($query) {
return $query->orderBy('price','asc');
})
->when($this->filter && $this->filter === 'Latest items', function ($query) {
return $query->orderBy('created_at','desc');
})
->when($this->min_price !=='' || $this->max_price !=='' , function($query)
{
return $query->whereBetween('price', [$this->min_price,$this->max_price]);
});
$items =$items->paginate(6);
return view('livewire.list-filter-items',compact('items','categories','tags','count_items'));
}
public function searchProducts()
{
$this->resetPage();
}
public function resetFilters()
{
$this->SelectedCat =[];
$this->SelectedSubCat =[];
$this->subcategories=[];
}
and my model
Categories
class ProductCategory extends Model implements HasMedia
{ use SoftDeletes; use InteractsWithMedia; use HasFactory;
public $table = 'product_categories';
protected $appends = [
'photo',
];
protected $dates = [
'created_at',
'updated_at',
'deleted_at',
];
protected $fillable = [
'name',
'description',
'created_at',
'updated_at',
'deleted_at',
];
public function registerMediaConversions(Media $media = null): void
{
$this->addMediaConversion('thumb')->fit('crop', 50, 50);
$this->addMediaConversion('preview')->fit('crop', 120, 120);
}
public function getPhotoAttribute()
{
$file = $this->getMedia('photo')->last();
if ($file) {
$file->url = $file->getUrl();
$file->thumbnail = $file->getUrl('thumb');
$file->preview = $file->getUrl('preview');
}
return $file;
}
public function subcategories()
{
return $this->belongsToMany(Subcategory::class);
}
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}
Model of subcategories
class Subcategory extends Model
{ use SoftDeletes; use HasFactory;
public $table = 'subcategories';
protected $dates = [
'created_at',
'updated_at',
'deleted_at',
];
protected $fillable = [
'name',
'created_at',
'updated_at',
'deleted_at',
];
public function subcategoriesProducts()
{
return $this->belongsToMany(Product::class);
}
public function subcategoriesProductCategories()
{
return $this->belongsToMany(ProductCategory::class);
}
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}
}
Please or to participate in this conversation.