首页 > 解决方案 > 如何在 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>

我怎样才能做到这一点?

标签: phplaravel

解决方案


将所有类别放在数组或集合上(例如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

推荐阅读