2016-12-25 66 views
0

我有一個下拉的問題,我總是得到第一個記錄。任何建議?從下拉菜單中選擇總是返回第一條記錄?

$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

請進行以下變化...

<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. 此外,您還必須添加選擇關鍵字在頁面加載作爲選擇標記的選項。
+0

即時得到錯誤未定義的變量請求 – uzhas

+0

@uzhas剛編輯我的答案。立即嘗試 – prateekkathal

+0

同樣的問題..數據庫中,它首先從下拉列表中保存我曾經選擇 – uzhas

相關問題