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>
我怎样才能做到这一点?
解决方案
将所有类别放在数组或集合上(例如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
推荐阅读
- c++ - decltype 的参数不正确
- mysql - Spark - 通过 Zeppelin EMR 连接到 mysql
- javafx - JavaFX CSS 两像素边框
- ruby-on-rails - 散列作为参数和默认参数值
- javascript - Javascript如何将值粘贴到用户名中
- java - 尽管我得到了正确的答案,但我一直被告知我在 Kattis 上的答案是错误的
- php - 如何在 PHP 的 3 页之间获取变量
- php - 数不清
- laravel - Laravel guzzle 身份验证不起作用
- buildbot - 当 BuildSlave 有几个 builder 时,你怎么知道哪个 builder 忙呢?