Niloy Rony
Niloy Rony

Reputation: 632

Laravel how to selected option value in loop when it will change?

I am new in laravel. I have written below code where category in a select box, process is not ajax. After sent get request how I will selected new value ?

<select name="company_category_id" class="form-control" id='category'>
     <option value="0">--Select Category--</option>
       @foreach ($categories as $category)
          <option value="{{ $category->id }}">{{ $category->name }}
      </option>
      @endforeach
 </select>

I have written javascript to sent get request in url

<script>
    $(document).ready(function() {
        $('#category').change(function() {
            url = window.location.href.split('?')[0];
            window.location.href = url + '?id=' + $(this).val();
        });
    });
</script>

   

Upvotes: 0

Views: 608

Answers (1)

Maulik Shah
Maulik Shah

Reputation: 1050

I suppose that you need preselected value once you submit form with above code. If so,

You just need to put condition and if match then attribute selected will be placed. Something like this,

<option value="{{ $category->id }}" $selectedId == $category->id ? 'selected' : ''>{{ $category->name }} </option>

$selectedId will be passed from controller to view

If I missed something please clarify your question

Upvotes: 1

Related Questions