foreach loop not working
can someone help me with this loop. it is not working. I am using livewire 3
<!-- Filter section, show/hide based on section state. -->
<div class="pt-6" id="filter-section-1">
<div class="space-y-4">
@foreach ($categories as $category)
<div class="space-y-4" wire:key="{{ $category->id }}">
<div class="flex items-center">
<input id="filter-category-2" name="category[]" value="{{ $category->id }}" type="checkbox" wire:model.live="selected_categories" id="{{ $category->slug }}" class="h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
<label for="{{ $category->slug }}" class="ml-3 text-sm text-gray-600">{{ $category->name }}</label>
</div>
</div>
@endforeach
</div>
</div>
</div>
<!-- Filter section, show/hide based on section state. -->
<div class="pt-6" id="filter-section-1">
<div class="space-y-4">
@foreach ($categories as $category)
<div class="space-y-4" wire:key="{{ $category->id }}">
<div class="flex items-center">
<input id="filter-category-2" name="category[]" value="{{ $category->id }}" type="checkbox" wire:model.live="selected_categories" id="{{ $category->slug }}" class="h-4 w-4 rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
<label for="{{ $category->slug }}" class="ml-3 text-sm text-gray-600">{{ $category->name }}</label>
</div>
</div>
@endforeach
</div>
</div>
</div>
1 Reply
public $categories = [];
public function mount()
{
// Fetch categories from the database
$this->categories = Category::all();
}
#[Url]
public $selected_categories = []; // Add this line to manage selected categories
// public $showCategoryFilter = false; // Add this line
//public function toggleCategoryFilter()
//{
// $this->showCategoryFilter = !$this->showCategoryFilter;
//}
public function render() {
$productQuery = Product::query()->where('is_visible', 1);
//'products' => Product::query()
// ->where('is_visible', 1)
// ->when(!empty($this->selectedCategories), function (Builder $query) {
// $query->whereHas('categories', function($query) {
// $query->whereIn('category_id', $this->selectedCategories);
// });
// })
// ->paginate(6),
return view('livewire.products-page', [
'products' => $productQuery->paginate(6),
'brands' => Brand::where('is_visible', 1)->get(['id', 'name', 'slug']),
'categories' => Category::where('is_visible', 1)->get(['id', 'name', 'slug']),
]);
}
}
public $categories = [];
public function mount()
{
// Fetch categories from the database
$this->categories = Category::all();
}
#[Url]
public $selected_categories = []; // Add this line to manage selected categories
// public $showCategoryFilter = false; // Add this line
//public function toggleCategoryFilter()
//{
// $this->showCategoryFilter = !$this->showCategoryFilter;
//}
public function render() {
$productQuery = Product::query()->where('is_visible', 1);
//'products' => Product::query()
// ->where('is_visible', 1)
// ->when(!empty($this->selectedCategories), function (Builder $query) {
// $query->whereHas('categories', function($query) {
// $query->whereIn('category_id', $this->selectedCategories);
// });
// })
// ->paginate(6),
return view('livewire.products-page', [
'products' => $productQuery->paginate(6),
'brands' => Brand::where('is_visible', 1)->get(['id', 'name', 'slug']),
'categories' => Category::where('is_visible', 1)->get(['id', 'name', 'slug']),
]);
}
}