[Bug] Backpack v5 breaks morphTo select field
See original GitHub issueBug report
What I did
I have a n-1 polymorphic relationship between “Intros” and “Companies” or “People”. Meaning an intro can link a user to either a company or a person. I have an ‘introable_id’ column in the intros table that points to the related model. The relationship is defined on App\Models\Intro as:
public function introable() { return $this->morphTo(); }
I have been using the select2 (1-n) relationship field in Laravel Backpack v4 without issues by just hardcoding the model that I want to default to from Backpack:
$this->crud->addField([ // Select2
'label' => "Intro To",
'type' => 'select2',
'name' => 'introable_id',
'entity' => 'introable',
'model' => "App\Models\Company",
'attribute' => 'name',
]);
What I expected to happen
I thought I could update to Laravel Backpack v5 and this would still work.
What happened
After updating, creating a new Intro or updating an existing one throws an error.
On create, I get:
Field 'introable_id' doesn't have a default value
even though I’ve double-checked that the request does indeed have introable_id set properly.
On update, I get:
Call to undefined method App\Models\Intro::introable_id()
The relationship is called ‘introable’ but the db column is called introable_id. This is thrown because line 99 in the Create trait is:
$relation = $item->{$relationMethod}();
What I’ve already tried to fix it
I tried to port over the v4 version of Select2 and that didn’t work. I verified that the request has the proper data and searched around a lotl
Is it a bug in the latest version of Backpack?
Yes
Backpack, Laravel, PHP, DB version
When I run php artisan backpack:version
the output is:
PHP VERSION:
PHP 8.1.1 (cli) (built: Dec 17 2021 22:38:05) (NTS) Copyright © The PHP Group Zend Engine v4.1.1, Copyright © Zend Technologies with Zend OPcache v8.1.1, Copyright ©, by Zend Technologies
LARAVEL VERSION:
v8.83.8@cf430301ad17656b3d918995bcdd0454c3c119b9
BACKPACK VERSION:
5.0.14@1e355c4c046a34423a1a3e3150120245a4bfd8e9
Issue Analytics
- State:
- Created a year ago
- Comments:13 (7 by maintainers)
Top GitHub Comments
@stevebeyatte , @adrienne I’ve just finished reviewing the new
morphTo
field inside repeatable. It is a BEAUTY: https://github.com/Laravel-Backpack/docs/pull/368So simple, yet so powerful.
It’s going through one more round of testing, by @jorgetwgroup . We expect to have it merged in 1-2 weeks, max.
Thank you for your patience 🙏
Hello @stevebeyatte
I am working on a core solution for the morph fields, I should have an mvp PR ready by tomorrow. In the meanwhile use
entity => false
on yourintroable_id
field.Let me know if that solves it for you.
Cheers