0

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?

11
  • You want to add all numbers for single user? Commented Nov 18, 2019 at 6:05
  • i want to add all the numbers sir in a single column in the sms table sir. Commented Nov 18, 2019 at 6:06
  • for the same user right? and also comma saperated? Commented Nov 18, 2019 at 6:06
  • yes sir. Its like this user_Id = 1, msg=random, mobile_no= 001,002,003, isSend=1 Commented Nov 18, 2019 at 6:08
  • use implode : 'mobile_number' => implode(',',$numbersArray), Commented Nov 18, 2019 at 6:08

3 Answers 3

2

Try this way

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

and you can directly implode() resulting array.

 $numbersArray = implode(',',$numbers);

You can skip foreach loop. $numbersArray will give comma separated string to directly save in column.

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

Comments

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


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

You can pluck just the phone column from the table and then call implode on the Collection to implode the values by ,.

Laravel 5.8 Docs - Query Builder - Retrieving Results - Retrieving A List Of Column Values pluck

Laravel 5.8 Docs - Collections - Available Methods - implode

13 Comments

it returns an error sir Array to string conversion (SQL: insert into sms` (user_id, school_id, msg, mobile_number, isSend) values (2, 1, New Event asda, +63 (928) 206-5706, 1))`
then your query had no results
implode is returning a string, so the error isn't coming from there
how do i select multiple columns from implode sir? I want to get also the name?
I have a documentation link for that method ... you are more than welcome to read it ... good luck with your project :)
|
1

You adding a json format to your 3rd column.

before you insert . json_encode it first then later once you retrieved the data . use json_decode and you are free to your object notation coding eg. $row->key

In addition if you insert json value to a column. you do JSON_VALUE query as well

https://learn.microsoft.com/en-us/sql/t-sql/functions/json-value-transact-sql?view=sql-server-ver15

https://learn.microsoft.com/en-us/sql/t-sql/functions/json-query-transact-sql?view=sql-server-ver15

1 Comment

json_encode your created json array again to make it array of json (converted to string) for saving purposes.

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.