Laravel-Backpack / CRUD

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

[Bug] When using 'select' type with morphToMany relationship, it errors. #5507

Closed NuktukDev closed 2 months ago

NuktukDev commented 2 months ago

Bug report

What I did

Created a field as follows:

CRUD::field('roles')
     ->type('select')
     ->allows_null(false)
     ->label('Role')
     ->entity('roles')
     ->model(\Spatie\Permission\Models\Role::class)
     ->attribute('name');

Each user in my system can be assigned to only one role.

What I expected to happen

I expected a drop down of all the entries for the Role model

What happened

I get the error:

Object of class Illuminate\Support\Collection could not be converted to int

which leads me to the file and line:

[vendor / backpack / crud / src / resources / views / crud / fields / select.blade.php : 38]

What I've already tried to fix it

I figured out that this line of code is the issue: @if($current_value == $connected_entity_entry->getKey()) $current_value in this situation is an empty collection, and so it can't be converted to an int value for the comparison. I fixed this by using a triple equal sign === instead, and it solved the issue in the view, but then another issue came to light.

Issue after the change stated above:

[vendor / backpack / crud / src / app / Library / CrudPanel / Traits / Create.php : 153]

array_values(): Argument #1 ($array) must be of type array, int given

Which leads to this code:

if (empty($relationValues)) {
    $relationValues = array_values($values);
}

in this scenerio $values is a single integer, not an array.

What I tried to fix this second issue:

Added [] to the end of the {{ $field['name'] }} of the name attribute on the select within vendor / backpack / crud / src / resources / views / crud / fields / select.blade.php:27 it now loos like:

<select
            name="{{ $field['name'] }}[]"
            @include('crud::fields.inc.attributes', ['default_class' => 'form-control form-select'])
            >

This fixed it, but obviously we can't have this for non morphToMany relationships.

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:
8.2.5

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

### LARAVEL VERSION:
10.48.8.0

### BACKPACK PACKAGE VERSIONS:
backpack/basset: 1.3.3
backpack/crud: 6.7.10
backpack/generators: v4.0.5
backpack/medialibrary-uploaders: 1.2.0
backpack/theme-tabler: 1.2.8
welcome[bot] commented 2 months ago

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:

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

karandatwani92 commented 2 months ago

If Each user can be assigned to only one role. You should use the belongsTo relationship.

To expect a Roles drop-down, you should use the select (1-n relationship) field.