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"
]
Solo Sam
  • 51
  • 1
  • 8

1 Answers1

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

Pedro X
  • 849
  • 5
  • 13
  • 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... – Solo Sam May 03 '22 at 08:36
  • 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 ? – Pedro X May 05 '22 at 09:12
  • 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... – Solo Sam May 05 '22 at 14:53
  • 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. – Solo Sam May 05 '22 at 15:10
  • Please join us on gitter https://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) – Pedro X May 05 '22 at 16:04