CRUD icon indicating copy to clipboard operation
CRUD copied to clipboard

[Feature Request] add support for `morphTo` inside `relationship` field

Open tabacitu opened this issue 3 years ago • 5 comments

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:

Screenshot 2022-01-18 at 11 34 39

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 in commentable_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 and relationship.morphToId fields;
  • [ ] when the commentable_type select is changed, commentable_id will have its options changed using JS that is stored inside the relationship.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.

tabacitu avatar Jan 18 '22 10:01 tabacitu

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).

tabacitu avatar Jan 18 '22 10:01 tabacitu

+1

stevebeyatte avatar Apr 15 '22 22:04 stevebeyatte

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.

tabacitu avatar Apr 20 '22 06:04 tabacitu

Any progress on this @tabacitu @pxpm ? It's a breaking problem for me!

adrienne avatar May 17 '22 08:05 adrienne

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 🎉

tabacitu avatar Aug 01 '22 16:08 tabacitu

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.

tsun424 avatar Aug 23 '22 23:08 tsun424

Hello @tsun424 Until we merge the core support for morph to you can use entity => false to disable backpack behavior. Cheers

pxpm avatar Aug 24 '22 07:08 pxpm

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.

tsun424 avatar Aug 24 '22 10:08 tsun424

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 🎉

tabacitu avatar Aug 29 '22 15:08 tabacitu