问题
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