从下拉列表中选择总是返回第一条记录?

时间:2016-12-25 10:33:32

标签: laravel laravel-5.2

我有下拉的问题,我总是得到第一个记录。有什么建议吗?

$category = $request->input('article_category');

<select class="form-control" name="article_category">
    <option value="" disabled>--Please select article category</option>
          @foreach ($categories as $category)
           <option value="{{ $category->id }}">{{ $category->title }}</option>
           @endforeach

1 个答案:

答案 0 :(得分:1)

请进行以下更改......

<select class="form-control" name="article_category">
  <option value="" disabled>--Please select article category</option>
  @foreach ($categories as $category)
    <option value="{{ $category->id }}" @if(request()->input('article_category') == $category->id){{ 'selected' }}@endif>{{ $category->title }}</option>
  @endforeach
</select>
  1. 您的$category = $request->input('article_category')被foreach写的foreach($categories as $category) 覆盖
  2. 此外,您必须添加所选关键字,以便在页面加载时将选项标记为已选中。