am working on a filtering system in Laravel PHP framework and I am trying to filter on products brand selected from a checkbox input.
When I try to retrieve Products lets say for example with the brand Intel which I have 3 product it works fine but when I try to filter on more then one value it's only receive the first selected checkbox item.
here is my view code:
<div id="filters-status" class="mt-3 collapse" aria-labelledby="filters-status-heading"
style="">
<ul class="space-y-6 mb-8">
@foreach ($prods->unique('marque') as $itme)
<li>
<label class="flex items-center cursor-pointer w-full">
<input type="checkbox" id="terms" name="marque[]"
value=""
class="h-5 w-5 mr-2 rounded border-jacarta-200 text-accent checked:bg-accent focus:ring-accent/20 focus:ring-offset-0 dark:border-jacarta-500 dark:bg-jacarta-600">
<span class="dark:text-white">
</span>
</label>
</li>
@endforeach
</ul>
<input
class="cursor-pointer rounded-full bg-accent-lighter w-full py-3 px-8 text-center font-semibold text-white transition-all hover:bg-accent-dark"
type="submit" value="Soumettre">
</div>```
enter image description here This are the brands checkbox
and this is my controller code:
public function fliter(Request $request)
{
$brand = ($request->marque);
$disponible = ($request->disponibilite);
$query = DB::table('produits');
if($brand){
$query->where('marque', $brand);
}
if($disponible) {
$query->where('disponibilite', $disponible);
}
$filtterProduits = $query->get();
return response()->json($filtterProduits);
}
this is the Json result if i select one brand
{
"id": 3,
"sous_categorie": 1,
"fournisseur_id": 1,
"ref": "104081",
"filenames": "[\"168591280568.webp\"]",
"description": "13th Generation Intel® Core™ i5 Processors 10Coeur 16 Threads Max Turbo",
"designation": "CORE I5 13400F PROCESSOR",
"prix_achat": 35000,
"prix_gros": 36800,
"prix_vent": 40000,
"marque": "INTEL",
"disponibilite": 0,
"quantite": 10,
"created_at": "2023-06-04 21:06:45",
"updated_at": "2023-06-11 20:15:11"
},
{
"id": 4,
"sous_categorie": 1,
"fournisseur_id": 2,
"ref": "103329",
"filenames": "[\"168616970297.webp\"]",
"description": "13th Generation Intel® Core™ i7 Processors\r\nTotal Cores 16\r\nTotal Threads 24\r\nMax Turbo Frequency\r\n5.40 GHz",
"designation": "CORE I7 13700KF PROCESSOR",
"prix_achat": 45000,
"prix_gros": 50000,
"prix_vent": 56400,
"marque": "INTEL",
"disponibilite": 0,
"quantite": 10,
"created_at": "2023-06-07 20:28:22",
"updated_at": "2023-06-11 20:15:21"
},
{
"id": 5,
"sous_categorie": 1,
"fournisseur_id": 2,
"ref": "103327",
"filenames": "[\"168616985056.webp\"]",
"description": "13th Generation Intel® Core™ i9 Processors\r\nTotal Cores 24\r\nTotal Threads 32\r\nMax Turbo Frequency 5.80 GHz",
"designation": "CORE I9 13900KF PROCESSOR",
"prix_achat": 74000,
"prix_gros": 74500,
"prix_vent": 82000,
"marque": "INTEL",
"disponibilite": 0,
"quantite": 15,
"created_at": "2023-06-07 20:30:50",
"updated_at": "2023-06-11 20:15:31"
}
as u can see 3 items
when I try to retrieve more than one brand it's only gets me the first checkbox selected values.
please don't give me negative comments I am new to Laravel, thanks in advance .
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/mBNrlLi
via
IFTTT