首先,当您为两个表创建迁移时,包含外键 (FK) 的表必须具有如下字段:
public function up(){
$table->increments('id');
$table->integer('customerID')->unsigned();
}
之后你需要调用下一个命令到控制台
php artisan migrate
接下来是下一条命令:
php arisan backpack:crud customers
php arisan backpack:crud transactions
之后,您需要在模型中定义从其他表返回值的函数。客户模型需要有下一个功能
public function transactions(){
return $this->hasMany('Transaction');
}
事务模型必须有下一个函数
public function customer() {
return $this->belongsTo('Customer');
}
接下来您必须在客户控制器中添加 CRUD 字段以显示
选择框中的交易。
$this->crud->addField([
'label' => 'Transactions', // Label for HTML form field
'type' => 'select2', // HTML element which displaying transactions
'name' => 'customerID', // Table column which is FK for Customer table
'entity'=> 'customer', // Function (method) in Customer model which return transactions
'attribute' => 'ID', // Column which user see in select box
'model' => 'Transaction' // Model which contain FK
]);
希望这会有所帮助:)