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.

[v5 Breaking Change] unable to store/update related field (one-to-one relationship)

See original GitHub issue

Bug 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()

image

I guess it is confused with the repeatable field (i.e. position for my case, it is now array but not json string).

image

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:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
pxpmcommented, Mar 4, 2022

@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 the getCasts() model method, otherwise we would be using getCasts 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 👏 🙏

2reactions
kiddtangcommented, Mar 4, 2022

Are you sure the use CrudTrait is still there on the your models?

OMG! Why I’ve never thought of it! Interestingly, it is totally working fine in v4.1…

I just add use CrudTrait; in EmployeeProfile everything back to normal! thanks @olipayne for your help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Breaking changes in EF Core 5.0 - Microsoft Learn
Complete list of breaking changes introduced in Entity Framework Core 5.0.
Read more >
Breaking changes in v5.6 - LLBLGen Pro
The following breaking changes have been in LLBLGen Pro Runtime Framework v3.5. They are a result of refactoring a lot of code of...
Read more >
Updating many-to-many relationships in EF Core 5 and above
In this book I build a book selling site, called Book App, where each book has two, many-to-many relationships: A direct many-to-many ...
Read more >
Why you Can't Save an Access Database After Updating a Field
This process includes documenting the linked relationships, breaking the existing relationships where the field being changed exists, ...
Read more >
How to Create a real one-to-one relationship in SQL Server
I'm pretty sure it is technically impossible in SQL Server to have a True 1 to 1 relationship, as that would mean you...
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