Laravel-Backpack / CRUD

Build custom admin panels. Fast!
https://backpackforlaravel.com
MIT License
3.17k stars 896 forks source link

[Bug] Relationship field are not displaying good selected values on setupUpdateOperation #5378

Closed Astriel closed 4 months ago

Astriel commented 11 months ago

Bug report

What I did

I've created a CrudController and the UpdateOperation with a relationship field from a 1 <> n relation.

 CRUD::field([
            'name'          => 'language_id',
            'type'          => 'relationship',
            'entity'        => 'language', // the method that defines the relationship in your Model
            'label'         => __('backpack.language'),
            'hint'          => __('backpack.create_group_language'),
        ]);

I've already checked all my relations, and they are working properly

What I expected to happen

On the update form / screen I was expecting to have the good ID selected in the relationship field.

If the field is : relationship, the first value of the select is selected, otherwise the good value is displayed

What happened

It is not working with the relationship field, but working with the select field. The field / value that is supposed to be selected is not the good one.

image

What I've already tried to fix it

Switching from relationship field to the select field.

        CRUD::field([
            'name'          => 'language_id',
            'type'          => 'select',
            'entity'        => 'language', // the method that defines the relationship in your Model
            'label'         => __('backpack.language'),
            'hint'          => __('backpack.create_group_language'),
        ]);

image

Is it a bug in the latest version of Backpack?

After I run composer update backpack/crud the bug is still there.

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

` ### PHP VERSION: PHP 8.2.11 (cli) (built: Sep 26 2023 15:25:31) (ZTS Visual C++ 2019 x64) Copyright (c) The PHP Group Zend Engine v4.2.11, Copyright (c) Zend Technologies

LARAVEL VERSION:

10.33.0.0

BACKPACK PACKAGE VERSIONS:

backpack/basset: 1.2.2 backpack/crud: 6.3.0 backpack/devtools: 2.0.3 backpack/generators: v4.0.2 backpack/logmanager: v5.0.1 backpack/permissionmanager: 7.1.1 backpack/pro: 2.0.18 backpack/theme-coreuiv2: 1.2.2 backpack/theme-coreuiv4: 1.1.1`

pxpm commented 11 months ago

Hey @Astriel what kind of relationship is that ? BelongsTo ?

Can you try just with the relation name as the field name to check if it does any difference ?

 CRUD::field('language')->label(__('backpack.language')->hint(__('backpack.create_group_language'));

Any chance you have a resources/views/vendor/backpack/pro/fields/relationship.blade.php custom in your app ?

I was about to ask you if you could reproduce it in our demo, and I noticed that in our demo the BelongsTo relationship was not properly working. I thought you were right, but in the end it was a mistake in our end.

We had two fields using the same relationship, so the value of one was overwriting the other.

I've fixed it in https://github.com/Laravel-Backpack/demo/pull/581 and belongsTo relations seems to be working just fine, so I will add that you could check:

Let me know if that helps.

Cheers

pxpm commented 4 months ago

Closing due to inactivity.

Please re-open if needed 🙏