如何在Laravel项目中的搜索表中选中搜索获取参数复选框?

时间:2018-08-17 19:32:18

标签: php laravel

我使用Laravel在电子商务网站中对产品进行过滤。

I have this url localhost:3000/?category=tops&color=black

我需要在过滤器搜索表单中通过“选中”来填充复选框:

<form action="{{ route('layouts.main') }}" method="GET">

<h4>Category</h4>
<label>
    <input type="checkbox" checked="" name="category[]" value="tops">
    Tops
</label>
<label>
    <input type="checkbox" name="category[]" value="bottoms">
    Bottoms
</label>

<h4>Color</h4>
<label>
    <input type="checkbox" checked="" name="category[]" value="black">
    Black
</label>
<label>
    <input type="checkbox" name="category[]" value="white">
    White
</label>

</form>

我该怎么做?

1 个答案:

答案 0 :(得分:0)

将所有类别放置在Array或Collection上(例如Category::all())并以此方式处理

@foreach($categories as $category)
   <label>
     <input {{ in_array(strtolower($category->name), Request::get('categories')) ? 
     'checked="checked"':
     '' }} 
     type="checkbox" name="category[]" value="{{ strtolower($category->name) }}">
     {{ $category->name }}
   </label>
@endforeach
相关问题