[Feature Request] add support for `morphTo` inside `relationship` field
See original GitHub issueFeature 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_id
that 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_select
for 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.morphTo
andrelationship.morphToId
fields; - when the
commentable_type
select is changed,commentable_id
will have its options changed using JS that is stored inside therelationship.morphTo_select
field; - if the dev wants to make the second select work with AJAX, they can replace it with a
relationship.morphTo_fetch
field (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.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (5 by maintainers)
Top GitHub Comments
+1
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 🎉