简体   繁体   中英

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. Let me explain what I'm doing / what I want:

I have "Folders" that have "Chapters" inside (so 1-n relation).

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.

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

More details herehttps://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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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