简体   繁体   English

Laravel Backpack - 内联创建,未在 DB 上添加关系

[英]Laravel Backpack - Inline create, relationship not being added on DB

I'm trying the 4.1 new feature "Inline create", but I can't seem to associate the ids of the items created.我正在尝试 4.1 新功能“内联创建”,但我似乎无法关联所创建项目的 ID。 Let me explain what I'm doing / what I want:让我解释一下我在做什么/我想要什么:

I have "Folders" that have "Chapters" inside (so 1-n relation).我有里面有“章节”的“文件夹”(所以 1-n 关系)。

My code:我的代码:

    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);
    }

It creates the main item and the related items no problem, but it doesn't actually relate them in the database at any point.它创建主要项目和相关项目没有问题,但它实际上并没有在任何时候将它们关联到数据库中。

Any clue of what I might be doing wrong?关于我可能做错了什么的任何线索? Followed the docs but can't seem to make it work.按照文档进行操作,但似乎无法正常工作。

Thank you.谢谢你。

Do you have the right column names in the db?你在数据库中有正确的列名吗? The columns that are making the relationship possible, ie in the folder table you should have a column named something like chapter_name or chapter_id, to identify the chapter where the folder belongs to.使关系成为可能的列,即在文件夹表中,您应该有一个名为 chapter_name 或 chapter_id 的列,以标识文件夹所属的章节。

Moreover, if those columns do not follow laravel conventions you need to add them as the second and third parameter when you are implementing the relationship in the models此外,如果这些列不遵循 laravel 约定,则需要在模型中实现关系时将它们添加为第二个和第三个参数

More details herehttps://laravel.com/docs/8.x/eloquent-relationships#one-to-many更多细节在这里https://laravel.com/docs/8.x/eloquent-relationships#one-to-many

One note on this... I was running into this issue and realized that I forgot to make the parent_id fillable on my child model.关于这个的一个注意事项......我遇到了这个问题并意识到我忘记让我的孩子 model 的parent_id可填写。

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
  'parent_id',
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM