laravel 错误:数组到字符串的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53532112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:10:48  来源:igfitidea点击:

laravel Error: Array to string conversion

phpsqllaraveljquery-select2

提问by Ali Anwar

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.

我已经完成了在 youtube、stakoverflow 和其他网站上可以找到的所有解决方案。我正在使用 Select2 添加多个角色,但我始终遇到相同的错误。

<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.

DD 函数显示完美结果,但之后显示错误。

enter image description hereIt works perfectly untill I add [] with the name="role_id[]". form action is as under.

在此处输入图片说明它可以完美运行,直到我添加 [] 名称为 =“role_id[]”。表单操作如下。

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!!');
}
如果我验证整数 'role_id'=> 'required|integer',它会显示错误 enter image description here在此处输入图片说明

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();
        });
    }

回答by Sagar Gautam

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

$request->role_id是数组,因此您不能将数组直接存储到数据库中,因此您可以使用以下内容,

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

Later you can use json_decodefunction to get array of role_id.

稍后您可以使用json_decode函数来获取 role_id 数组。

回答by Emtiaz Zahid

Problem:

问题:

$request->role_idis an array. not a single data.

$request->role_id是一个数组。没有一个数据。

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);

回答by Leena Patel

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

由于您有角色数据透视表,因此您的用户表中不需要 role_id 列

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!!');
}

回答by Junaid Abbas

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

你不能“回显”一个数组,这是错误。

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

您需要收集数组中的行,然后返回该数组

回答by Adam

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

如果你想为你的.js代码输出一个数组,你可以使用

@json($array)

@json($array)

since Laravel 5.5

从 Laravel 5.5 开始