lundi 18 novembre 2019

how do i do a multiple insert in laravel 5.5?

I want to do a multiple insert in my laravel project but i dont know how to do it. Do i need to loop it or there are any other method to do it? Im a bit confused. I am using querybuilder btw. Does anyone know how to do it? I need some help. Thanks.

Controller code.

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

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

            dd($numbers);


            $sms = DB::table('sms')->insert([
                'user_id' => $sid->id,
                'school_id' => $sid->school_id,
                'msg' => 'New Event '. $request->name,
                'mobile_number' =>  $numbersArray,
                'name' => 'Sample',
                'isSend' => 1
            ]);



        }

dump result

Collection {#552 ▼
  #items: array:5 [▼
    0 => {#550 ▼
      +"phone": "+63 (928) 206-5706"
      +"name": "Ayden Kutch"
    }
    1 => {#568 ▼
      +"phone": "(0817) 447-1492"
      +"name": "Ruthie Quigley"
    }
    2 => {#567 ▼
      +"phone": "+63 (920) 203-3874"
      +"name": "Alaina O'Kon"
    }
    3 => {#569 ▶}
    4 => {#570 ▶}
  ]
}

I want to insert it in my sms table like this format

+----+---------+---------------+---------------+--------+------------+------------+-----------+-------------+
| id | user_id | msg           | mobile_number | isSend | created_at | updated_at | school_id | name        |
+----+---------+---------------+---------------+--------+------------+------------+-----------+-------------+
|  8 |      13 | New Event    |     123456     |      1 | NULL       | NULL       |         2 | Ayden Kutch |
+----+---------+---------------+---------------+--------+------------+------------+-----------+-------------+

In every name and number. It generates another row.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire