php 如何筛选检索到的多个复选框值?

6ojccjat  于 2023-06-21  发布在  PHP
关注(0)|答案(1)|浏览(105)

我在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>```

enter image description here这是品牌复选框
这是我的控制器代码:

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的新手,提前感谢。

b4lqfgs4

b4lqfgs41#

由此改变

$query->where('marque', $brand)

到这一点,使您的查询可以搜索所有选定的品牌

$query->whereIn('marque', $brand)

相关问题