Getting selected option on edit form in laravel
Asked Answered
C

5

11

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() }}
Castilian answered 23/7, 2017 at 18:21 Comment(0)
D
34

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>
Dollar answered 23/7, 2017 at 18:40 Comment(1)
it works thanks, but what about users how i compare that?Castilian
D
1

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>
Downcome answered 23/7, 2017 at 18:51 Comment(0)
C
0

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

Coot answered 8/3, 2021 at 12:12 Comment(0)
I
0

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>
Impudicity answered 9/3, 2022 at 9:11 Comment(0)
L
0

it's very simple:

                    <select class="form-control" name="category_id" required="">
                      <option value="">Select Category</option>
                      @foreach ($categories as $category)
                        <option value="{{ $category->id }}" {{ old('category_id', $post->category_id) == $category->id ? ' selected' : '' }}> {{ $category->title }}</option>
                      @endforeach
                    </select>
Lashkar answered 15/7, 2024 at 12:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.