2

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

         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

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

2
  • looks like $datas['role_id'] is an array, since you build $roles in the foreach Commented Sep 12, 2017 at 10:11
  • Can you please explain it briefly ?? Commented Sep 12, 2017 at 10:15

3 Answers 3

1

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

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Could you accept this as your answer if it's working?
1

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

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

2 Comments

Doesn't this only insert the last row and forgets the rest?
For that you have to arraypush
0

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;  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.