dimanche 17 novembre 2019

Laravel 5.5 querybuilder insert to table from array collection

How can I insert the collection into the database. I loop the values in my controller and when I do insert, It has some error in it Array to string conversion (SQL: insert intosms(user_id,msg,mobile_number,isSend) values (1, SMS, {"phone":"+63 (971) 659-8143"}, 1)). Can someone tell me what should I do about this? Thanks in advance

Controller Code

   if($request->smsn == 'on'){
            $events->smsnotify = 1;

            $numbers = \DB::table('users')
                        ->select('phone')
                        ->where('school_id', '=', $sid)
                        ->where('role', '=', $request->group_id)
                        ->get();

            $numbersArray = [];
            foreach($numbers as $number){
                $numbersArray[] = json_encode($number);
            }

                        // dd($numbersArray);
            $sms = DB::table('sms')->insert([
                'user_id' => $sid,
                'msg' => $request->name,
                'mobile_number' => $numbersArray,
                'isSend' => 1
            ]);

        }

when i dd($numbersArray);

array:9 [▼
  0 => "{"phone":"+63 (971) 659-8143"}"
  1 => "{"phone":"(0997) 212-7919"}"
  2 => "{"phone":"(0900) 117-9012"}"
  3 => "{"phone":"(0905) 470-0661"}"
  4 => "{"phone":"+63 (977) 194-0623"}"
  5 => "{"phone":"(0927) 448-8047"}"
  6 => "{"phone":"+63 (923) 889-3009"}"
  7 => "{"phone":"+63 (971) 215-1217"}"
  8 => "{"phone":"+63 (998) 452-5708"}"
]

I just want to insert the numbers with a comma separator in a single row of my table. How could I achieve that?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire