laravel Error: Array to string conversion

雨燕双飞 提交于 2020-03-21 06:43:35

问题


I have gone through every solution I could find over youtube, stakoverflow and other websites. I am using Select2 to add multiple roles but I a consistently getting the same error.

<select id="role" name="role_id[]" multiple='multiple' 
        class="form-control js-example-basic-multiple">
    @foreach($roles as $role)
        <option value="{{$role->id}}">{{$role->name}}</option>
    @endforeach
</select>

DD function is showing perfect result but after that it shows error.

It works perfectly untill I add [] with the name="role_id[]". form action is as under.

public function store(Request $request)
{
    $this->validate($request, [
        'name'=> 'required|string|max:225',
        'status'=> 'required',
        'role_id'=> 'required',
        'email'=> 'required|string|email|max:225|unique:users',
        'password'=> 'required|string|min:6|confirmed'
    ]);

    $password = Hash::make($request->password);
    // dd($request->all());

    $user = new User;
    $user->name = $request->name;
    $user->status = $request->status;
    $user->role_id = $request->role_id;
    $user->email = $request->email;
    $user->password = $password;
    $user->remember_token;
    $user->save();
    // $user->roles()->sync($request->roles, false);
    return back()->with('message', 'User added successfully!!');
}
If I validate for integer 'role_id'=> 'required|integer', it shows error

Migration is as under

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->unsignedInteger('role_id')->default(1);
            $table->boolean('status')->default(0);
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

回答1:


As you have pivot table for roles than you dont need role_id column in your users table

public function store(Request $request)
{
$this->validate($request, [
    'name'=> 'required|string|max:225',
    'status'=> 'required',
    'role_id'=> 'required|array',
    'email'=> 'required|string|email|max:225|unique:users',
    'password'=> 'required|string|min:6|confirmed'
]);

$password = Hash::make($request->password);
// dd($request->all());

$user = new User;
$user->name = $request->name;
$user->status = $request->status;
$user->email = $request->email;
$user->password = $password;
$user->remember_token;
$user->save();

foreach($request->input('role_id') as $role)
{
   $user->assign($role);
}
// $user->roles()->sync($request->roles, false);
return back()->with('message', 'User added successfully!!');
}



回答2:


$request->role_id is array so you can't store array to database directly so you can use following,

  $user->role_id = json_encode($request->role_id);

Later you can use json_decode function to get array of role_id.




回答3:


Problem:

$request->role_id is an array. not a single data.

Suggestion:

Here two things

You allocating multiple rules with an user

You should not store your rules in users table

You should do that like this:

create another table to store users role

and store user rules separately

Example:

      $user = new User;
      $user->name = $request->name;
      $user->status = $request->status;
      $user->email = $request->email;
      $user->password = $password;
      $user->remember_token;
      $user->save();

      $roleAssigns = [];
      foreach($request->role_id as $role){
          $roleAssigns[] = [
              'role_id' => $role,
              'user_id' => $user->id
          ]
      }
      //UserRole is the model of user_roles table
      UserRole::insert($roleAssigns);



回答4:


You cannot "echo" an array, that is the error.

you will need to collect the lines in an array, and then return that array




回答5:


If you want to output an array for your .js code, you may use

@json($array)

since Laravel 5.5



来源:https://stackoverflow.com/questions/53532112/laravel-error-array-to-string-conversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!