Laravel 5.4 中的数组到字符串转换,控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46173560/
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 16:38:41  来源:igfitidea点击:

Array to string conversion in laravel 5.4 , controllers

phplaravel

提问by user7346035

I'm getting array to conversion error while inserting multiple data , code which i mentioned below please take a look and somebody help me

我在插入多个数据时遇到数组转换错误,我在下面提到的代码请看一下,有人帮助我

Controller.php

控制器.php

         if(!empty($value))
                {
                    foreach ($value as $v) 
                    {   
                        $insert[] = ['name' => $v['name'], 'email' => $v['email'],'company_name' => $v['company_name'],'company_id' => $v['company_id'], 'emp_id' => $v['emp_id']];     
                         $role_id= $v['role_id'];
                         $name=$v['name'];
                         $email=$v['email'];
                         $emails[]=$v['email'];
                         $emp_id=$v['emp_id'];                      
                         $data = array( 'name' => $name,'email' => $email , 'emp_id' => $emp_id);
                         $roles[]= $v['role_id']; 

                    }
                }
            }

            if(!empty($insert))
            {
                 $inserted=User::insert($insert);
                 if($inserted)
                 {
                     $email_select=User::select('id')->whereIn('email',$emails)->where('company_id',Auth::user()->company_id)->orderBy('id','Asc')->get();
                    foreach ($email_select as $key => $idget) 
                    {
                        $getid[]=$idget->id;
                    }
                 }
                 $datas[]=['user_id' => $getid , 'role_id' => $roles];                  
                 $insert_role=DB::table('role_user')->insert($datas) ;

I'm getting error called array to string conversion while insert_role variable execution

我在 insert_role 变量执行时收到称为数组到字符串转换的错误

(2/2) QueryException Array to string conversion (SQL: insert into role_user(role_id, user_id) values (1, 16))

(2/2) QueryException 数组到字符串的转换(SQL:insert into role_user( role_id, user_id) values (1, 16))

采纳答案by Mark Walet

$rolesand $getidare both arrays. I am guessing you want to assign all roles to all selected mail addresses. Then you would have to do the following:

$roles$getid都是数组。我猜您想将所有角色分配给所有选定的邮件地址。然后,您必须执行以下操作:

if($inserted)
{
    $email_select=User::select('id')->whereIn('email',$emails)->where('company_id',Auth::user()->company_id)->orderBy('id','Asc')->get();
    foreach ($email_select as $key => $idget) 
    {
        foreach($roles as $role) {
            $datas[] = ['user_id' => $idget->id, 'role_id' => $role];
        }
    }
}
$insert_role=DB::table('role_user')->insert($datas);

I think this should work.

我认为这应该有效。

回答by User123123

Make $roles[]= $v['role_id'];to $roles= $v['role_id'];

$roles[]= $v['role_id'];$roles= $v['role_id'];

And $getid[]=$idget->id;to $getid=$idget->id;

并且 $getid[]=$idget->id;$getid=$idget->id;

回答by Akram Wahid

instead of assigning in array, you are trying to assign as string, that is what causing you the error, possible fix is.

您不是在数组中分配,而是尝试分配为字符串,这就是导致错误的原因,可能的解决方法是。

$roles[]= $v['role_id']; 
$getid[]=$idget->id;