uzhas
uzhas

Reputation: 925

Select from dropdown always return first record?

I have a problem with dropdown, i always get first record. Any suggestion?

$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

Upvotes: 0

Views: 97

Answers (1)

prateekkathal
prateekkathal

Reputation: 3572

Please make the following change...

<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. Your $category = $request->input('article_category') was getting overwritten by the foreach($categories as $category) written in foreach.
  2. Also, you must add the selected keyword to mark the option as selected on page load.

Upvotes: 1

Related Questions