依赖于另一个字段的select2字段 [英] select2 field that depends on another field
本文介绍了依赖于另一个字段的select2字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想实现本教程中显示的代码:
I want to implement the code show in this tutorial:
但是,此行返回 null
值:
$ form = collect($ request-> input('form'))-> pluck('value','name');
我不知道我是否应该在这里使用 input('form')
,因为它取自3.4版本的文档.有人知道它是否可以在版本4中使用.
I don't know if i should use input('form')
here as it is taken from the documentation for version 3.4 Does someone know if it works in version 4.
CrudController:
CrudController:
// CRUD::field('sub_district_id')
// ->type('select2')
// ->label('Kecamatan')
// ->entity('sub_district')
// ->attribute('sub_district_name')
// ->model('App\Models\SubDistrict')
// ->wrapper(['class' => 'form-group col-md-6']);
CRUD::field('sub_district_id')
->type('relationship')
->label('Kecamatan')
->attribute('sub_district_name')
->include_all_form_fields(true)
->wrapper(['class' => 'form-group col-md-6']);
CRUD::field('village_id')
->type('select2_from_ajax')
->label('Desa/Kelurahan')
->entity('village')
->attribute('village_name')
->model("App\Models\Village")
->wrapper(['class' => 'form-group col-md-6'])
->data_source(url('api/village'))
->placeholder('Pilih Desa/Kelurahan')
->minimum_input_length(0)
->dependencies(['sub_district_id'])
->method('GET'); // optional - HTTP method to use for the AJAX call (GET, POST)
ApiController:
ApiController:
$search_term = $request->input('q');
$form = collect($request->input('form'))->pluck('value', 'name');
// dump($form);
$options = Village::query();
if (!$form['sub_district_id']) {
return [];
}
// if a category has been selected, only show articles in that category
if ($form['sub_district_id']) {
$options = $options->where('sub_district_id', $form['sub_district_id']);
}
if ($search_term) {
$results = $options->where('village_name', 'LIKE', '%' . $search_term . '%')->paginate(10);
} else {
$results = $options->paginate(10);
}
return $options->paginate(10);
推荐答案
只需将此行添加到村庄
的字段
Just add this line to village
's field
-> include_all_form_fields(true)
,它将返回表单中的所有值
and it will return all the value from the form
CrudController:
CrudController:
// ...
CRUD::field('village_id')
->type('select2_from_ajax')
->label('Desa/Kelurahan')
->entity('village')
->attribute('village_name')
->model("\App\Models\Village")
->wrapper(['class' => 'form-group col-md-6'])
->data_source(url('api/village'))
->placeholder('Pilih Desa/Kelurahan')
->minimum_input_length(0)
->dependencies(['sub_district_id'])
->include_all_form_fields(true)
->method('GET'); // optional - HTTP method to use for the AJAX call (GET, POST)
// ...
这篇关于依赖于另一个字段的select2字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文