Getting selected option on edit form in laravel

Viewed 34085

I have an editable form for my website orders and I have these fields:

User quantity note status

I also have other options in this form, but only these fields are important to me in order to be able to get the default values.

For example, I want to be able to see the amount of quantity that a user has ordered by default and then I could change it or leave it alone. Currently all my drop-down values start from the first value and not what the user has chosen.

How can I do that?

This is my form:

{{ Form::model($order, array('route' => array('orders.update', $order->id), 'method' => 'PUT', 'files' => true)) }}

                <div class="form-group">
                  <label class="col-md-3 control-label" for="type">Order ID</label>
                  <div class="col-md-9 inputGroupContainer">
                    <div class="input-group">
                      <span class="input-group-addon"><i class="fa fa-file-word-o"></i></span>
                      <input class="form-control" type="text" name="" value="{{ $order->id }}" readonly>
                    </div>
                  </div>
                </div>


                <div class="form-group">
                  <label class="col-md-3" for="invoice_nu">Invoice Number:</label>
                  <div class="col-md-9 inputGroupContainer">
                    <div class="input-group">
                      <span class="input-group-addon"><i class="fa fa-sort-numeric-asc"></i></span>
                      <input class="form-control" type="text" name="invoice_nu" value="{{ $order->invoice_nu }}" readonly>
                    </div>
                  </div>
                </div>

                <div class="form-group">
                  <label class="col-md-3 control-label" for="type">User</label>
                  <div class="col-md-9 inputGroupContainer">
                    <div class="input-group">
                      <span class="input-group-addon"><i class="fa fa-user-o"></i></span>
                      <select class="form-control" id="type" name="user_id">
                        @foreach($users as $user)
                            <option value="{{ $user->id }}">{{ $user->name }}</option>
                        @endforeach
                      </select>
                    </div>
                  </div>
                </div>



                <div class="form-group">
                  <label class="col-md-3 control-label" for="type">Quantity</label>
                  <div class="col-md-9 inputGroupContainer">
                    <div class="input-group">
                      <span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
                      <select class="form-control" id="type" name="quantity">
                        <option value="">Select Quantity</option>
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                      </select>
                    </div>
                  </div>
                </div>

                <div class="form-group">
                  <label class="col-md-3 control-label" for="type">Note</label>
                  <div class="col-md-9 inputGroupContainer">
                    <div class="input-group">
                      <span class="input-group-addon"><i class="fa fa-file-word-o"></i></span>
                      <textarea name="name" class="form-control" rows="8">@if(!empty($order->note)){{ $order->note }}@else-@endif</textarea>
                    </div>
                  </div>
                </div>


                <div class="form-group">
                  <label class="col-md-3 control-label" for="type">Status</label>
                  <div class="col-md-9 inputGroupContainer">
                    <div class="input-group">
                      <span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
                      <select class="form-control" id="type" name="status">
                        <option value="Waiting Payment">Waiting Payment</option>
                        <option value="Paid">Paid</option>
                      </select>
                    </div>
                  </div>
                </div>


                {{ Form::submit('Save', array('class' => 'btn btn-primary mt-20')) }}

                {{ Form::close() }}
4 Answers

You can also use Blade Directives - If Statements. For a field like is_visible with two options you'd do something like this

<div class="form-group{{ $errors->has('is_visible') ? ' has-danger' : '' }}">
    <label class="form-control-label" for="input-is_visible">{{ __('Is Visible') }}</label>
    <select  class="form-control{{ $errors->has('is_visible') ? ' is-invalid' : '' }}" id="is_visible" name="is_visible">
    @if (auth()->user()->is_visible == 0)
        <option value="">-</option>
        <option value="0" @if (old('is_visible') == 0) @endif selected>{{ __('No') }}</option>
        <option value="1" @if (old('is_visible') == 1) @endif>{{ __('Yes') }}</option>
    @else
        <option value="">-</option>
        <option value="0" @if (old('is_visible') == 0) @endif>{{ __('No') }}</option>
        <option value="1" @if (old('is_visible') == 1) @endif selected>{{ __('Yes') }}</option>
    @endif
    </select>    
    @include('alerts.feedback', ['field' => 'is_visible'])
</div>

enter image description here

Having two array is the better way. One array is the list of items and the second is the last selected items. Just use array_search for looking and selecting same items.

   <select name="quantitys[]" id="quantitys" multiple>
      @foreach($quantitys as $_)
        <option value="{{ $_->id }}"
        {{ array_search($_->id,$lastQuantitys) ? 'selected':'' }}>
                    {{ $_->id }}
        </option>
      @endforeach
   </select>
Related