Laravel Backpack - Undefined array key "relation_type"

Viewed 447

Laravel v9.9.0, Backpack v5.0.14, Backpack Pro v1.0.13

I'm getting an error when I try to save changes in Laravel Backpack CRUD: Undefined array key "relation_type"

Through trial and error, I've pinned the cause down to one of the fields defined in SetupCreateOperation (a standard Backpack method).

ListingCrudController setupCreateOperation:

CRUD::field('listing_category_id')
    ->label('Category')
    ->type('select2')
    ->entity('listingCategory')
    ->model("App\Models\ListingCategory")
    ->attribute('category_name')
    ->size(6)
    ->tab('Main');

I don't have any guards in play that would affect this, and believe the relationship is defined properly in the model.

Listing:

public function listingCategory()
{
    return $this->belongsTo(ListingCategory::class);
}

Other fields are defined in a very similar way, and they all work as expected. The only difference (that I can see) is that this is the only entity/method name that is camel cased, but I've tried renaming 'listingCategory' to 'category'. I've also tried using 'select' instead of 'select2', neither edit made any difference.

I believe I could add relationship_type to the field definition in the controller but my (admittedly limited) understanding of the Backpack internals is that I shouldn't do this.

For clarity, the CRUD views (both create and update) seem fine and all of the options are populated for this field, it's only upon save that I see the error. If I remove the field from the setUpCreateOperation everything works just fine.

If relevant, this is all running in a local dev environment (PHP 8.0) on Windows 10.

I'd appreciate ANY suggestions as to what I'm doing wrong, or how to go about identifying the root issue.

Edit: output of dd() as requested...

"listing_category_id" => array:8 [▼
    "name" => "listing_category_id"
    "entity" => "listingCategory"
    "label" => "Category"
    "type" => "select2"
    "model" => "App\Models\ListingCategory"
    "attribute" => "category_name"
    "wrapper" => array:1 [▼
      "class" => "form-group col-md-6"
    ]
    "tab" => "Main"
]
1 Answers

since you are using PRO, can you just use:

CRUD::field('listingCategory')
    ->label('Category')
    ->attribute('category_name')
    ->size(6)
    ->tab('Main');

If you are validating listing_category_id in request please validate the relation name listingCategory. It should work just fine.

Cheers

Related