Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Can't change the model attribute when using "relationship" crud field on create crud #5252

Closed
Astriel opened this issue Aug 1, 2023 · 7 comments
Labels

Comments

@Astriel
Copy link

Astriel commented Aug 1, 2023

Bug report

What I did

I've generated a CrudController with the php artisan backpack:build command
On the CrudController, in the setupCreateOperation method, I've set a "relationship" field which is corresponding to a many to many relation. This works fine and I'm able to select it on the screen, but when I want to change the "attribute" to showcase another value from my model it's not working.

    CRUD::field([
        'name'        => 'myrelation',
        'type'          => 'relationship',
        'tab'           => __('backpack.ats'),
        'model'         => 'App\Models\V0\Mymodel',
        'label'         => __('backpack.firsttab'),
        'attribute'     => 'version', // The problem is that here, whatever I try to type, it's always fetching for the name attribute on my model
         'pivot'         => true,
        'subfields'   => [
            [
                'name' => 'first_field',
                'label'      => __('backpack.first_field'),
                'type'       => 'text',
                'tab'        => __('backpack.firsttab'),
                'hint'       => __('backpack.first_field_hint'),
                'wrapper' => [
                    'class' => 'form-group col-md-6',
                ],
            ],
        ],
    ]);

What I expected to happen

With the following code and the following field define, I would expect to have the "version" attribute used into the select input generated with the crud.

What happened

Whatever I try, the CRUD field is always going to fetch for the "name" attribute of my model.

What I've already tried to fix it

I've already tried to switch to other fields such as

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.1.2 (cli) (built: Jan 19 2022 10:18:23) (ZTS Visual C++ 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies

### LARAVEL VERSION:
10.16.1.0

### BACKPACK PACKAGE VERSIONS:
backpack/basset: 1.0.2
backpack/crud: 6.1.1
backpack/devtools: 2.0.3
backpack/generators: v4.0.2
backpack/permissionmanager: 7.0.0
backpack/pro: 2.0.10
backpack/theme-coreuiv4: 1.0.5
@Astriel Astriel added the triage label Aug 1, 2023
@welcome
Copy link

welcome bot commented Aug 1, 2023

Hello there! Thanks for opening your first issue on this repo!

Just a heads-up: Here at Backpack we use Github Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.

Backpack communication channels:

  • Bug Reports, Feature Requests - Github Issues (here);
  • Quick help (How do I do X) - Gitter Chatroom;
  • Long questions (I have done X and Y and it won't do Z wtf) - Stackoverflow, using the backpack-for-laravel tag;
  • Showing off something you've made, asking for opinion on Backpack/Laravel matters - Reddit;

Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.

Thank you!

--
Justin Case
The Backpack Robot

@Astriel
Copy link
Author

Astriel commented Aug 1, 2023

Sounds like it's really similar to the following issue #5220

@pxpm
Copy link
Contributor

pxpm commented Aug 1, 2023

Hey @Astriel

If you need an attribute in a select field (as subfield) you should define the attribute in the subfield. There is no point in defining the attribute in the "parent field" as it will not apply to any subfield.

From what you shared I am guessing you are working with pivot fields, in that case, you should set the attribute in the pivotSelect key, that allow you to configure the pivot field. https://backpackforlaravel.com/docs/6.x/crud-how-to#extra-saving-additional-attributes-to-the-pivot-table

I am going to close this as I don't think there is any bug here.

Conversation is not locked, if you think I am wrong (and I am wrong a lot of times) please reply, and if needed we will re-open.

Cheers

@pxpm pxpm closed this as completed Aug 1, 2023
@Astriel
Copy link
Author

Astriel commented Aug 1, 2023

Hey @pxpm !

Actually the attribute is not for the subfield. The attribute parameter is for the main field. I'm using the pro plugin and the "relationship" type of input. In my case, I've got a n - n relationship and I want to add several models of one entity to another one.

If I'm defining the crud field (not a subfield) with the following code, and I pass the attribute parameter, it works well :

        CRUD::field([
            'name'          => 'mymodel',
            'type'          => 'relationship',
            'tab'           => 'mytab',
            'model'         => 'App\Models\V0\MyModel\MyModel',
            'label'         => 'the label'
            'attribute'     => 'version',
]);

But as soon as start to add subfields to fill pivot values like on this demo page : https://demo.backpackforlaravel.com/admin/monster/create#relationship the 'attribute' is not longer taken in account, and it's going to fetch for the "name" attribute on my model and not "version".

@pxpm
Copy link
Contributor

pxpm commented Aug 1, 2023

Like I said to you, and pointed in the last comment. In a n-n relationship, you should configure the attribute in your pivotSelect field. What I am missing, did you tried it ?

CRUD::field([
        // adding here for clarity:
        'pivotSelect' => [
            'attribute' => 'version'
        ]
        'name'        => 'myrelation',
        'type'          => 'relationship',
        'tab'           => __('backpack.ats'),
        'model'         => 'App\Models\V0\Mymodel',
        'label'         => __('backpack.firsttab'),
         'pivot'         => true,
        'subfields'   => [
            [
                'name' => 'first_field',
                'label'      => __('backpack.first_field'),
                'type'       => 'text',
                'tab'        => __('backpack.firsttab'),
                'hint'       => __('backpack.first_field_hint'),
                'wrapper' => [
                    'class' => 'form-group col-md-6',
                ],
            ],
        ],
    ]);

Let me know if that doesn't help.

Thanks 🙏

@Astriel
Copy link
Author

Astriel commented Aug 1, 2023

Sounds perfect ! Problem solved, thank you so much.

Just a quick last question, my model name is "ats". I've defined the CRUD::setEntityNameStrings('ats', 'ats');
But on the relationship input field, the S at the end is always deleted ?

@pxpm
Copy link
Contributor

pxpm commented Aug 1, 2023

Can you provide a screenshot or something of what you mean ?

My guess is that when you setEntityNameStrings (single/plural) we use the Laravel singular/plural helpers and it converts the singular of ats to at.

Let me know! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

No branches or pull requests

2 participants