CRUD icon indicating copy to clipboard operation
CRUD copied to clipboard

[Bug] relations field don`t work with resolveRelationUsing method

Open rezahmady opened this issue 1 year ago • 1 comments
trafficstars

Bug report

What I did

i create a package to add relation to main Video Model with resolveRelationUsing method:

image

this is my field :

image

??

What I expected to happen

The field works correctly

What happened

image

??

What I've already tried to fix it

I added model or entity attribute to filed array but the error still remains.

When you add relation in Video model everything is good

??

Backpack, Laravel, PHP, DB version

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

### PHP VERSION:
8.3.6

### PHP EXTENSIONS:
Core, bcmath, calendar, ctype, date, filter, hash, iconv, json, SPL, pcre, random, readline, Reflection, session, standard, mysqlnd, tokenizer, zlib, libxml, dom, PDO, openssl, SimpleXML, xml, xmlreader, xmlwriter, curl, ftp, fileinfo, gd, intl, mbstring, exif, mysqli, Phar, pdo_mysql, xsl, zip

### LARAVEL VERSION:
10.48.20.0

### BACKPACK PACKAGE VERSIONS:
backpack/basset: 1.3.5
backpack/crud: 6.7.26
backpack/editable-columns: 3.0.10
backpack/filemanager: 3.0.8
backpack/generators: v4.0.5
backpack/logmanager: v5.0.2
backpack/pro: 2.2.13
backpack/revise-operation: 2.0.0
backpack/theme-coreuiv2: 1.2.5

rezahmady avatar Aug 28 '24 05:08 rezahmady

Hey @rezahmady thanks for the report 🙏

We've just tagged backpack/crud 6.7.32 with a fix for dynamic relationships it should work just as any other relation defined on the model.

Let me know if you are still experiencing problems after the update 👍

Cheers

pxpm avatar Aug 28 '24 15:08 pxpm

Thank you very much, @pxpm 🙏. Everything is correct except in the belongsToMany with pivot

this is my relation :

Video::resolveRelationUsing('crew_people', function (Video $videoModel) {
        return $videoModel->belongsToMany(CrewPeople::class,'crew_people_video', 'video_id', 'crew_people_id')
                ->withPivot(['crew_role_id', 'id', 'star']);
});

When displaying the form in editing mode, it works correctly, but when saving, it gives this error:

Undefined array key "App\Providers\{closure}"

this is my field:

[
    'name'          => 'crew_people',
    'type'          => "relationship",
    'label' => '',
    'allow_duplicate_pivots' => true,
    'new_item_label'  => trans('crew::lang.add_crew_people'),
    'tab' => trans('crew::lang.crew_people_plural'),
    'subfields'   => [
        [
            'name' => 'crew_role_id',
            'label' => trans('crew::lang.crew_role_singular'),
            'type' => "select2_from_array",
            'options' => CrewRole::pluck('name', 'id')->toArray(),
            'wrapper' => [
                'class' => 'form-group col-md-4',
            ],
            'placeholder' => trans('crew::lang.crew_role_plural_placeholder'),
        ],
        [
            'name' => 'star',
            'label' => trans('crew::lang.crew_star'),
            'type' => "switch",
            'wrapper' => [
                'class' => 'form-group col-md-2 pt-5',
            ],
        ]
    ],
    'pivotSelect'=> [
        // 'attribute' => "title", // attribute on model that is shown to user
        'label' => trans('crew::lang.crew_people_singular'),
        'placeholder' => trans('crew::lang.crew_placeholder'),
        'wrapper' => [
            'class' => 'col-md-6',
        ],
        'inline_create' => true,
        // note that just like any other select, enabling ajax will require you to provide an url for the field
        // to fetch available options. You can use the FetchOperation or manually create the enpoint.
        'ajax' => true,
        'minimum_input_length'    => 0,
        'data_source' => url('/api/crew/people'),
    ],
]

rezahmady avatar Aug 29 '24 07:08 rezahmady

I think I missed adding a test for belongsToMany with subfields.

I will add one ASAP and fix anything I found broken 👍 thanks for the heads up!

Cheers

pxpm avatar Aug 29 '24 13:08 pxpm

Thanks @rezahmady Indeed it failed in the specific scenario BelongsToMany -> subfields -> allow_duplicate_pivots 🤷‍♂️ Sorry I've missed it.

I've released backpack/crud 6.7.33 with the fixes for it. Let me know if you are still experiencing issues after the update.

I would like to ask you to open a new issue in that case, as I can unsubscribe from closed issues and may miss your comment.

Thanks again 🙏

pxpm avatar Aug 29 '24 14:08 pxpm

Thank @pxpm 🙏 Everything is good.

rezahmady avatar Aug 30 '24 07:08 rezahmady