我在数据库中有libraries
,townships
和states
表(Library
, Township
,State
作为模型名称)。
Library
是 1-n 到Township
。
在Library
.
public function township(){
return $this->belongsTo('App\Township', 'township_id');
}
在Township
模型中
public function libraries(){
return $this->hasMany('App\Library');
}
并且Township
是 1-n 到State
。
在Township
模型中
public function state() {
return $this->belongsTo('App\State', 'state_id');
}
在State
模型中
public function townships(){
return $this->hasMany('App\Township');
}
在图书馆的创建表单中,我需要从中获取所有行State
并将其填充到 select2 框中。当然,还有另一个 select2 框用于Township
. 它的值会在 selected 上改变State
。
所以,问题是我需要在我的LibraryCrudController.php
$this->crud->addField([
'label' => 'State',
'type' => 'select2',
'name' => 'state_id', //Should From Township Model. But how?
'entity' => 'state',
'attribute' => 'name',
'model' => "App\State",
]);
如果我这样做了,错误是
ErrorException in SchemaException.php line 86:
There is no column with name 'state_id' on table 'libraries'. (View: /home/zugor/code/backpack-crud/vendor/backpack/crud/src/resources/views/fields/select2.blade.php) (View: /home/zugor/code/backpack-crud/vendor/backpack/crud/src/resources/views/fields/select2.blade.php) (View: /home/zugor/code/backpack-crud/vendor/backpack/crud/src/resources/views/fields/select2.blade.php) (View: /home/zugor/code/backpack-crud/vendor/backpack/crud/src/resources/views/fields/select2.blade.php)
有什么解决方案吗?我搜索了很多,但没有找到。