在laravel中编辑表单上选择选项

时间:2017-07-23 18:21:46

标签: php forms laravel

我的网站订单有一个可编辑的表单,我有以下字段:

User quantity note status

我在此表单中还有其他选项,但只有这些字段对我来说很重要才能获得默认值。

例如,我希望能够看到用户默认订购的数量,然后我可以更改它或不管它。目前,我的所有下拉值都是从第一个值开始,而不是用户选择的值。

我该怎么做?

这是我的表格:

{{ 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() }}

3 个答案:

答案 0 :(得分:4)

您可以将$order->quantity与选项值进行比较,以添加selected属性

<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>

用户将$user->id$order->user_id(或其他根据您的数据)进行比较

<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>

答案 1 :(得分:0)

检查您是否具有quatity,然后将所选属性添加到相应的选项。

        <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>

答案 2 :(得分:0)

您也可以使用 Blade Directives - If Statements。对于像 is_visible 这样有两个选项的字段,你可以做这样的事情

<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