2

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 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Pedro, but doing so produces a text input (I need it to be a select) prepopulated with the id of the related entity rather than the category name. If I add the type attribute back in, I get an error Undefined array key "model". Adding the model attribute leads to Undefined array key "entity" etc...
That's odd! I've just tested: public function MyUser() { return $this->belongsTo(\App\User::class); CRUD::field('MyUser')->label('User')->attribute('name')->size(6)->tab('Main'); It shows me a select with the user list, as it's supposed to. Are you sure you are not overriding some backpack core files? After you define the field, can you do dd($this->crud->fields()) and post the output ?
It's really odd! I've got numerous other CRUD forms all working as expected... I'm a complete noob to stack overflow. Bear with whilst I figure out how to format the dd() output in a comment! Will edit shortly...
I've edited the original question to add the dd() output. I don't think I've overridden anything important; I've edited the save buttons as the default options weren't quite right for the application, but that's all I can think of (I've tried using stock buttons but it made no difference). The application was was originally running backpack v4. Perhaps I missed something in the upgrade process? I'd be surprised if this was the case though as surely it would be affecting all the CRUD forms, not just this field in this form.
Please join us on gitter gitter.im/BackpackForLaravel/Lobby and ping me. @pxpm there. so we can continue this conversation. Something must be wrong indeed, you should be getting a type => relationship and get a type => select2 (that should also be a select and not a text input like you are getting)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.