question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
stevebeyattecommented, Apr 15, 2022

+1

0reactions
tabacitucommented, Aug 29, 2022

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 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix error "Please use whereHasMorph() for MorphTo ...
I have found solution by overriding the following three functions in the CommentCrudController : search() applySearchTerm($searchTerm) ...
Read more >
Polymorphic relationships in Laravel and their use cases
Here's an overview of polymorphic relationships in Laravel, including use cases where polymorphic relationships are especially applicable.
Read more >
Eloquent: Relationships - The PHP Framework For Web Artisans
But, before diving too deep into using relationships, let's learn how to define each type of relationship supported by Eloquent. One To One....
Read more >
New Features for Querying Polymorphic Relations in Laravel ...
The Laravel team released v5.8.27 with a whereHasMorph() method to work with polymorphic Eloquent relationships (MorphTo).
Read more >
Relationships | Laravel Orion - GitHub Pages
Routes, unlike controllers, are defined in a different way for each relationship type. <?php use Illuminate\Support\Facades\Route; ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found