繁体   English   中英

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

[英]How can I fill search form with checkboxed by search get-parameters in Laravel project?

我使用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>

我怎样才能做到这一点?

将所有类别放到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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM