[英]Laravel Backpack - Inline create, relationship not being added on DB
我正在嘗試 4.1 新功能“內聯創建”,但我似乎無法關聯所創建項目的 ID。 讓我解釋一下我在做什么/我想要什么:
我有里面有“章節”的“文件夾”(所以 1-n 關系)。
我的代碼:
CRUD::addField([ //Folder crud
'name' => 'chapters',
'type' => 'relationship',
'label' => 'Unidad',
'model' => "App\Models\Chapter",
'inline_create' => [
'entity' => 'chapter',
'modal_class' => 'modal-dialog modal-xl',
'modal_route' => route('chapter-inline-create'),
'create_route' => route('chapter-inline-create-save'),
]
]);
protected function setupCreateOperation() //Chapter crud
{
CRUD::setValidation(ChapterRequest::class);
CRUD::addField([
'name' => 'name',
'type' => 'text',
'label' => 'Nombre'
]);
}
public function chapters() //Folder model
{
return $this->hasMany(Chapter::class);
}
public function folder() //Chapter model
{
return $this->belongsTo(Folder::class);
}
它創建主要項目和相關項目沒有問題,但它實際上並沒有在任何時候將它們關聯到數據庫中。
關於我可能做錯了什么的任何線索? 按照文檔進行操作,但似乎無法正常工作。
謝謝你。
你在數據庫中有正確的列名嗎? 使關系成為可能的列,即在文件夾表中,您應該有一個名為 chapter_name 或 chapter_id 的列,以標識文件夾所屬的章節。
此外,如果這些列不遵循 laravel 約定,則需要在模型中實現關系時將它們添加為第二個和第三個參數
更多細節在這里https://laravel.com/docs/8.x/eloquent-relationships#one-to-many
關於這個的一個注意事項......我遇到了這個問題並意識到我忘記讓我的孩子 model 的parent_id
可填寫。
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'parent_id',
]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.