CRUD
CRUD copied to clipboard
[Feature Request] add support for `morphTo` inside `relationship` field
Feature Request
What's the feature you think Backpack should have?
Ability to define a relationship field for a morphTo relationship. Take the examples we've just added to the demo (Petshop). You can assign Comments to Pets and Owners... but if you create a CRUD for comments... the closest you can get (by default) is this:
But this isn't really helpful to the admin. As a dev, you have to
- manually create a select for
commentable_type, showing "Pet" and "Owner" - create a custom field
commentable_idthat will show either Pets or Owners, depending on what's selected incommentable_type
I think we should add a new field to relationship, let's call it relationship.morphTo or something. This new field would show (on the same line):
- a select for the
commentable_type - a select2 for the
commentable_id(when type is changed, the options inside this one are changed too)
As a developer, how would you use this? Most of the time you'd just need to define
CRUD::field('commentable')->models([
\App\Models\Pet::class,
\App\Models\Owner::class,
]);
Behind the scenes, it will use that relationship.morphTo field we talked about, which will setup two subfields:
- commentable_type (a simple select)
- commentable_id (a custom select2, let's call this one
relationship.morphTo_selectfor now, for clarity)
So what it will ACTUALLY DO is the same thing as if the dev defined:
CRUD::field('commentable')->morphTypeField([
'name' => 'commentable_type',
'type' => 'select',
'options' => [
'App\Models\Pet' => 'Pet',
'App\Models\Owner' => 'Owner',
],
])->morphIdField([
'name' => 'commentable_id',
'type' => 'relationship.morphTo_select',
'models' => [
\App\Models\Pet::class,
\App\Models\Owner::class,
]
]);
The idea behind the above is to:
- [ ] make zero changes to the Backpack core;
- [ ] add
relationship.morphToandrelationship.morphToIdfields; - [ ] when the
commentable_typeselect is changed,commentable_idwill have its options changed using JS that is stored inside therelationship.morphTo_selectfield; - [ ] if the dev wants to make the second select work with AJAX, they can replace it with a
relationship.morphTo_fetchfield (which will switch the AJAX URL it points to);
Have you already implemented a prototype solution, for your own project?
No
Do you see this as a core feature or an add-on?
Core, but not urgent.
Before we do any of the above, we should talk more about this, think of all the implications and basically write the docs for it. It's a cool feature, but fewer than 1% of all devs will ever need this, so it's low-prio. It'll be a complicated one though, so it's a lot of time spent to cater to the needs of very few people. Let's work smart on this one (not hard).
+1
Heads up @pxpm - we have a good description for what you're working on here. Please reference it in your PR so we check these ideas too.
Any progress on this @tabacitu @pxpm ? It's a breaking problem for me!
Progress report: Pedro has created a PR for this, that adds this. But it only supports simple selects (non-AJAX):
- https://github.com/Laravel-Backpack/CRUD/pull/4330
- https://github.com/Laravel-Backpack/PRO/pull/18
- https://github.com/Laravel-Backpack/demo/pull/388
He's now working on adding AJAX support (and polishing the syntax). We expect to have a solution launched for this by the end of the month 🎉
Hi team, I used to have this relationship, morphTo working on V4, currently working on upgrading to V5 and the page is broken with 500 error The relationship field does not support MorphTo at the moment, nobody asked for it yet, cannot go ahead for this V5 upgrade, so keen on getting this resolved/supported as soon as possible, thanks.
Hello @tsun424
Until we merge the core support for morph to you can use entity => false to disable backpack behavior.
Cheers
Hi @pxpm , thanks for your response.
Adding entity => false triggered another error: Undefined array key "relation_type".
Currently from V5, if a field is defined as relationship type in a CrudController and it is morphTo relationship in the model, just got 500 error, whereas, it was working on V4.1, I'm feeling this is a regression.
Let's close this in favor of https://github.com/Laravel-Backpack/CRUD/pull/4579 - moving the conversation there as we're suuuper close to merging it 🎉