提问人:MOHAMED KESSOUM 提问时间:6/14/2023 更新时间:6/14/2023 访问量:108
如何筛选检索到的多个复选框值?
How can I filter on Multiple Checkbox values retrieved?
问:
我正在 Laravel PHP 框架中开发一个过滤系统,我正在尝试过滤从复选框输入中选择的产品品牌。
当我尝试检索产品时,例如,我有 3 个产品的品牌英特尔,它工作正常,但是当我尝试过滤多个值时,它只收到第一个选定的复选框项。
这是我的观点代码:
<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="{{ $itme->marque }}"
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">
{{ $itme->marque }}
</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>```
在此处输入图像描述这是品牌复选框
这是我的控制器代码:
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);
}
如果我选择一个品牌,这是 Json 结果
{
"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"
}
如您所见 3 个项目
当我尝试检索多个品牌时,它只会让我获得第一个复选框选择值。
请不要给我负面评论,我是Laravel的新手,提前致谢。
答:
0赞
Xun
6/14/2023
#1
从这里改变
$query->where('marque', $brand)
为此,以便您的查询可以搜索所有选定的品牌
$query->whereIn('marque', $brand)
评论
0赞
MOHAMED KESSOUM
6/14/2023
效果很好,谢谢 👍
评论