1

我在数据库中有libraries,townshipsstates表(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)

有什么解决方案吗?我搜索了很多,但没有找到。

4

0 回答 0