Php 在JSON数据源中使用select2_from_ajax
我是新来的拉雷维尔和背包的拉雷维尔,所以请容忍我。我正在尝试创建一个客户注册表单,该表单具有一个“State”字段,该字段根据为“Country”字段选择的值动态填充 我遵循背包作者提供的说明: 州和国家/地区均来自此数据集:。它们作为集合返回,但不会从数据库中读取 客户端的表架构(简化) 好戏 “国家”领域运作良好。它在创建时从JSON数据集获取数据,并在更新/保存时从数据库读取/写入信息:Php 在JSON数据源中使用select2_from_ajax,php,laravel,backpack-for-laravel,Php,Laravel,Backpack For Laravel,我是新来的拉雷维尔和背包的拉雷维尔,所以请容忍我。我正在尝试创建一个客户注册表单,该表单具有一个“State”字段,该字段根据为“Country”字段选择的值动态填充 我遵循背包作者提供的说明: 州和国家/地区均来自此数据集:。它们作为集合返回,但不会从数据库中读取 客户端的表架构(简化) 好戏 “国家”领域运作良好。它在创建时从JSON数据集获取数据,并在更新/保存时从数据库读取/写入信息: // ClientCrudController.php $countries = new Count
// ClientCrudController.php
$countries = new Countries();
$allCountriesCodes = $countries->all()->pluck('name.common', 'cca2')->toArray();
$this->crud->addField([
'name' => 'country_iso_cca2',
'label' => 'Country',
'type' => 'select2_from_array',
'options' => $allCountriesCodes,
'allows_null' => false,
'default' => 'US',
]);
坏的(不完整的)部分
调用/admin/client/create
将导致错误(“调用未定义的方法App\Models\client::foobar”)
我理解出现错误是因为没有为状态定义模型,因此没有关系。我的问题是,我不理解在这样的情况下实现应该是什么样子,在这种情况下,两个select字段在ORM级别不代表单独的实体
是否可以以“背包原生”的方式实现这种依赖关系,而不必创建自定义字段类型?Hmm。。。如果在addField()语句中将
foobar
替换为state
,怎么样?@tabacitu在这种情况下,我会得到“调用未定义的方法\App\Models\Client::state()”嗯。。。如果在addField()语句中将foobar
替换为state
,怎么样?@tabacitu在这种情况下,我得到“调用未定义的方法\App\Models\Client::state()”
// ClientCrudController.php
$countries = new Countries();
$allCountriesCodes = $countries->all()->pluck('name.common', 'cca2')->toArray();
$this->crud->addField([
'name' => 'country_iso_cca2',
'label' => 'Country',
'type' => 'select2_from_array',
'options' => $allCountriesCodes,
'allows_null' => false,
'default' => 'US',
]);
// ClientCrudController.php
$this->crud->addField([
'name' => 'state',
'label' => 'State',
'type' => 'select2_from_ajax',
'entity' => 'foobar', // the method that defines the relationship in your Model
'attribute' => 'name',
'data_source' => url('api/states'),
'placeholder' => 'Select a country first...',
'dependencies' => ['country_iso_cca2'],
]);