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() }}
To change the selected option in an HTML <select> element we have to change the value property on the <select> element or the selected property on the <option> element we want to get selected.
Example 1. Step 1 − Execute the following command to proceed with the same. Step 2 − This will add HTML package to Laravel as shown in the following image. Step 3 − Now, we need to add the package shown above to Laravel configuration file which is stored at config/app.
you can compare $order->quantity
with option value to add selected
attribute
<select class="form-control" id="type" name="quantity">
<option value="">Select Quantity</option>
<option value="1" {{ $order->quantity == 1 ? 'selected' : '' }}>1</option>
<option value="2" {{ $order->quantity == 2 ? 'selected' : '' }}>2</option>
<option value="3" {{ $order->quantity == 3 ? 'selected' : '' }}>3</option>
<option value="4" {{ $order->quantity == 4 ? 'selected' : '' }}>4</option>
<option value="5" {{ $order->quantity == 5 ? 'selected' : '' }}>5</option>
</select>
for user compare $user->id
with $order->user_id
(or something else according your data)
<select class="form-control" id="type" name="user_id">
@foreach($users as $user)
<option value="{{ $user->id }}" {{ $user->id == $order->user_id ? 'selected' : '' }}>{{ $user->name }}</option>
@endforeach
</select>
Check if you have quatity and then add selected attribute to respective option.
<select class="form-control" id="type" name="quantity">
@if($order->quantity)
<option value="{{$order->quantity}}" selected>{{$order->quantity}}</option>
@else
<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>
@endif
</select>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With