我的更新表单中有一个选择框,其中包含不同的付款方式。我需要选择多个数据。如果用户已在表中具有付款类型,则获取数据并在selectbox中加载。我添加了这些功能,但是在下拉列表中多次显示数据。请找到我附上的图片。
实际上用户有三种付款方式。下拉列表显示这些付款类型三次。
请检查下面给出的代码并纠正我。
$cabin->payment_type = payment type from user table
$cabinInfo->paymentType() = Generating array in controller
update.blade.php
@inject('cabinInfo', 'App\Http\Controllers\Cabinowner\DetailsController')
<select id="payment" name="payment" class="form-control payment" multiple="multiple" data-placeholder="Choose payment type" style="width: 100%;">
@foreach($cabin->payment_type as $payment)
@foreach($cabinInfo->paymentType() as $paymentTypeKey => $paymentType)
<option value="{{ $paymentTypeKey }}" @if($paymentTypeKey == $payment || old('payment') == $payment) selected="selected" @endif>{{ $paymentType }}</option>
@endforeach
@endforeach
</select>
脚本
/* Multiple select for payment */
$(".payment").select2();
UpdateController.php
public function index()
{
$cabin = Cabin::where('id', Auth::user()->id)
->first();
return view('update', ['cabin' => $cabin]);
}
public function paymentType()
{
$array = array(
'0' => "Cash",
'1' => "Debit Card",
'2' => "Credit Card",
);
return $array;
}
答案 0 :(得分:1)
我假设$cabin->payment_type
返回一个数组?如果是这样你的逻辑应该更像这样:
@inject('cabinInfo', 'App\Http\Controllers\Cabinowner\DetailsController')
<select id="payment" name="payment" class="form-control payment" multiple="multiple" data-placeholder="Choose payment type" style="width: 100%;">
@foreach($cabinInfo->paymentType() as $paymentTypeKey => $paymentType)
<option value="{{ $paymentTypeKey }}" @if(in_array($paymentTypeKey, $cabin->payment_type )|| in_array(old('payment'), $cabin->payment_type )) selected="selected" @endif>{{ $paymentType }}</option>
@endforeach
</select>