Can't add field from different table in laravel-backpack CRUD

拜拜、爱过 提交于 2020-01-03 01:48:05

问题


I have libraries, townships and states tables in the database (Library, Township, State as model names). Library is 1-n to Township .

In the Library.

public function township(){
    return $this->belongsTo('App\Township', 'township_id');
  }

In the Township Model

public function libraries(){
        return $this->hasMany('App\Library');
    }

And Township is 1-n to State.

In the Township Model

public function state()  {
        return $this->belongsTo('App\State', 'state_id');
    }

In the State Model

public function townships(){
        return $this->hasMany('App\Township');
    }

In the Library's create form, I need to get all rows from State and fill it to select2 box. Ofcourse, there's another select2 box for Township. It's value will change on selected State.

So, the question is I need to add a field like this in my 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",
   ]);

If I did like this,the Error is

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)

Is there any solution for this ? I searched much but found none.

来源:https://stackoverflow.com/questions/42849145/cant-add-field-from-different-table-in-laravel-backpack-crud

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!