Javascript 如何使用select2和laravel Collective填充laravel blade视图中的其他字段
我想使用以下代码使用Laravel Collective Select和Select2从控制器检索数据:Javascript 如何使用select2和laravel Collective填充laravel blade视图中的其他字段,javascript,php,laravel,jquery-select2,laravelcollective,Javascript,Php,Laravel,Jquery Select2,Laravelcollective,我想使用以下代码使用Laravel Collective Select和Select2从控制器检索数据: public function create() { $customers=Customer::all(); $customerAlt= Customer::get()->pluck('customer_name', 'id')->prepend('Please Select', ''); return view('req.create', compact 'c
public function create() {
$customers=Customer::all();
$customerAlt= Customer::get()->pluck('customer_name', 'id')->prepend('Please Select', '');
return view('req.create', compact 'customer', 'customerAlt')
}
现在在我的刀片视图中,如果使用标准形式,我想使用像这样的Laravel集合
<select id="NamaPelanggan" name="nama_pelanggan" class="form-control">
<option>Pilih Satu</option>
@foreach($customers as $cus)
<option data-contact="{{ $cus->contact_person }}" data-phone="{{ $cus->phone_number }}" value="{{$cus->id}}">{{$cus->name}}</option>
@endforeach
</select>
问题是我如何使用laravelcollective withselect2来替换上面的普通select,以及我应该使用哪个
$customer
,将您的select元素替换为laravelcollective等效元素,并将select2 css类作为属性传递,如下所示:
{!! Form::select('customer', $customers, null, ['class' => 'select2']) !!}
Customer必须是一个二维数组,key=>value,其中key是html选项值,value是html选项标签
$customers = [
1 => 'Customer A',
2 => 'Customer B',
]
---编辑---
要向选项字段(如data contact和data phone)添加额外属性,可以将其作为数组作为第五个参数传递
请注意:第五个参数是一个数组,具有options元素的属性;第四个参数是和数组,带有用于选择元素的属性
LaravelCollective select元素允许您将第五个参数作为一个数组传递,该数组由您的select值索引,并且在每个索引中必须有另一个带有额外属性的数组。因此,您将需要2个阵列来实现所需的功能
$customers
作为值和标签传递李>
$extraParameters
,其中每个索引都是前一个数组的值$customers = [
1 => 'Customer A',
2 => 'Customer B',
]
这应该是你的第二次
$extraParameters = [
1 => ['data-contact' => 'Customer A Contact', 'data-phone' => 'Customer A Phone'],
2 => ['data-contact' => 'Customer B Contact', 'data-phone' => 'Customer B Phone']
]
将select元素替换为等效的laravelcollective元素,并将select2 css类作为属性传递,如下所示:
{!! Form::select('customer', $customers, null, ['class' => 'select2']) !!}
Customer必须是一个二维数组,key=>value,其中key是html选项值,value是html选项标签
$customers = [
1 => 'Customer A',
2 => 'Customer B',
]
---编辑---
要向选项字段(如data contact和data phone)添加额外属性,可以将其作为数组作为第五个参数传递
请注意:第五个参数是一个数组,具有options元素的属性;第四个参数是和数组,带有用于选择元素的属性
LaravelCollective select元素允许您将第五个参数作为一个数组传递,该数组由您的select值索引,并且在每个索引中必须有另一个带有额外属性的数组。因此,您将需要2个阵列来实现所需的功能
$customers
作为值和标签传递李>
$extraParameters
,其中每个索引都是前一个数组的值$customers = [
1 => 'Customer A',
2 => 'Customer B',
]
这应该是你的第二次
$extraParameters = [
1 => ['data-contact' => 'Customer A Contact', 'data-phone' => 'Customer A Phone'],
2 => ['data-contact' => 'Customer B Contact', 'data-phone' => 'Customer B Phone']
]
但是我如何在laravelcollective中传递这个数据联系人=“{{$cus->contact\u person}}”呢?但是我如何在laravelcollective中传递这个数据联系人=“{{$cus->contact\u person}”?