z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.12k stars 2.81k forks source link

Multi-select replaced by select for roles[column roles not working] in UserController. But it's show error #5785

Open Shahjalal7311 opened 1 year ago

Shahjalal7311 commented 1 year ago

Description:

I am trying to remove multiselect and add only select in Usercontroller. But it's not working.The error is

Array to string conversion (View: project/encore/laravel-admin/resources/views/form.blade.php)

image

Steps To Reproduce:

public function form()
    {
        $userModel = config('admin.database.users_model');

        $roleModel = config('admin.database.roles_model');

        $form = new Form(new $userModel);

        $form->select('roles', trans('admin.roles'))->options($roleModel::all()->pluck('name', 'id'));

        return $form;
    }

What's i am doing wrong. Any one can help me to find the solution.

Yanghsuanming commented 1 year ago
add ->config('maximumSelectionLength', 1);

$form->multipleSelect('roles', trans('admin.roles'))->options($roleModel::all()->pluck('name', 'id'))->config('maximumSelectionLength', 1);
technilogics commented 10 months ago

@Yanghsuanming thank you!

I also fix cascade linking issue adding a belongsToMany relation i.e.

`public function adminRole() { return $this->belongsToMany(AdminRole::class, 'admin_role_users', 'user_id', 'role_id');

}`

Above and your suggested method i.e. ->config('maximumSelectionLength', 1); works well in CREATE/Adding but unfortunately when EDIT it produce error:

ViewException In CanCascadeFields.php line 163 : addslashes(): Argument #1 ($string) must be of type string, array given (View: /vendor/encore/laravel-admin/resources/views/form.blade.php)

on line 163, that function is defined ` /**

i debug and found that $this->value() return array

array:1 [▼ // vendor/encore/laravel-admin/src/Form/Field/CanCascadeFields.php:163 0 => 5 ]

If i modify and return $value=$this->value(); return addslashes(old($this->column(), $this->$value[0]));//i.e. 5

it works, but i donot want to change core file.

Any help please!