[v5 Breaking Change] unable to store/update related field (one-to-one relationship)
See original GitHub issueBug report
What I did
update from Backpack 4.1 to v5
What I expected to happen
After the upgrade, this functionality breaks…
My Models:
Employee — hasOne profile()
—> EmployeeProfile
Employee <— belongsTo employee()
— EmployeeProfile
My CRUD field:
$this->crud->addFields([
[
'name' => 'profile.address_1',
'label' => 'Address (Line 1)',
'type' => 'text',
'tab' => 'Profile',
'wrapper' => ['class' => 'form-group col-md-12']
],
[
'name' => 'profile.address_2',
'label' => 'Address (Line 2)',
'type' => 'text',
'tab' => 'Profile',
'wrapper' => ['class' => 'form-group col-md-12']
],
[
'name' => 'profile.postal_code',
'label' => 'Postal Code',
'type' => 'text',
'tab' => 'Profile',
'wrapper' => ['class' => 'form-group col-md-4']
],
[
'name' => 'profile.city',
'label' => 'City',
'type' => 'text',
'tab' => 'Profile',
'wrapper' => ['class' => 'form-group col-md-4']
],
]);
What happened
The Error: Call to undefined method App\Models\EmployeeProfile::getCastedAttributes()
I guess it is confused with the repeatable
field (i.e. position
for my case, it is now array
but not json
string).
So, in v5… I am unable to directly store the fields directly to another model anymore?
What I’ve already tried to fix it
My workaround for now, in case it is a breaking change
public function store()
{
$profile = request('profile'); // extract the profile array
$this->crud->setRequest($this->crud->validateRequest());
$this->crud->setRequest($this->removeProfileInput($this->crud->getRequest())); // remove profile array
$this->crud->unsetValidation(); // validation has already been run
$response = $this->traitStore(); // commit the employee
$this->crud->entry->profile()->updateOrCreate($profile); // create or update the employee's profile
return $response;
}
Is it a bug in the latest version of Backpack?
After I run composer update backpack/crud
the bug… is it still there?
Yes
Backpack, Laravel, PHP, DB version
When I run php artisan backpack:version
the output is:
PHP VERSION:
PHP 8.0.16 (cli) (built: Feb 21 2022 14:42:00) ( NTS ) Copyright © The PHP Group Zend Engine v4.0.16, Copyright © Zend Technologies with Zend OPcache v8.0.16, Copyright ©, by Zend Technologies
LARAVEL VERSION:
v8.83.2@b91b3b5b39fbbdc763746f5714e08d50a4dd7857
BACKPACK VERSION:
5.0.9@d995de2026b6fcb1b13b469f371c929c29d1c1cc
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top GitHub Comments
@kiddtang nice to know you got it working! Indeed checking for model casts was introduced in v5, and that’s a specific CrudTrait function. The only reason for it to exists is because of spatie-translatable. Totally unrelated I know, but that made us create the
getCastedAttributes
function to overcome the fact that spatie overwrittes thegetCasts()
model method, otherwise we would be usinggetCasts
here, that is a Model function and not our custom function in CrudTrait.If you are manually creating the models I agree that this comes with an added weight, but let me tell you if you didn’t know yet, you don’t need to manually save relations. if you are only doing it because it didn’t properly work in 4.1, in v5 Backpack we did alot of improvements in this matter and Backpack can now take care of it for you, https://backpackforlaravel.com/docs/5.x/crud-how-to#hasone-1-1-relationship
Thanks @olipayne for pitching in 👏 🙏
OMG! Why I’ve never thought of it! Interestingly, it is totally working fine in v4.1…
I just add
use CrudTrait;
inEmployeeProfile
everything back to normal! thanks @olipayne for your help!