laravel-backpack CRUD中不能从不同的表中添加字段

时间:2017-03-17 04:17:43

标签: laravel laravel-5 laravel-5.4 backpack-for-laravel

我在数据库中有librariestownshipsstates个表(LibraryTownshipState作为型号名称)。 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框。 当然,Township还有另一个select2框。它的值将在选定的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)

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

1 个答案:

答案 0 :(得分:1)

我对Backpack Fileds的看法不对。

适用于custom view type