3

what i want to achieve is that inside the DB query i insert all the records in one go so that the query doesn't run many times. the following code doesn't allow me to use the for loop inside the array. kindly help.

if($user=="MasterDealer")
        {
            if($request['accessibleTodealers']!='' || $request['accessibleTodealers']!=null)
            {
                $memberships=explode(",",$request['accessibleTodealers']);
                $membe = array();
                $now = Carbon::now();
                DB::table('accessible_membership_logs')->insert([
                    array(
                        for ($i=0; $i <count($memberships) ; $i++){
                        array(
                            ['membership_id']=$memberships[$i],
                            ['masterdealer_id']=$dealer,
                            ['dealer_id'] = null,
                            ['user'] = 'masterdealer',
                            ['operation'] = 'Accessible',
                            ['admin_id'] = $data['id'],
                            ['created_at'] = $now->format('Y-m-d H:i:s'),
                            ['updated_at'] = $now->format('Y-m-d H:i:s')
                        )}
                    )
                ]);
                Alert::message("","Changes Updated.!","success");
                return redirect()->intended('/admin/masterDealerDetail/'.$dealer); 
            }
        }
Muzammil Baloch
  • 176
  • 3
  • 14
  • You can't do that. It's an `insert` query, it is meant to be executed as many times as the number of records that need to be inserted. – linuxartisan Apr 10 '18 at 07:27
  • @linuxartisan but that would be a lot of stress on the DataBase, i dont want to execute the query that many times. is there any other solution? – Muzammil Baloch Apr 10 '18 at 07:33
  • hi you can create a table and put the object in that array, after that you can make **saveMany()**. it's more good and safefull. – Steve Ruben Apr 10 '18 at 07:37
  • @SteveRuben can you explain a bit more? – Muzammil Baloch Apr 10 '18 at 07:48
  • @MuzammilBaloch Sorry about that. As it turns out, you can add multiple records. Refer this [how-do-i-insert-multiple-rows-without-repeating-the-insert-into-dbo-blah-part](https://stackoverflow.com/questions/2624713/how-do-i-insert-multiple-rows-without-repeating-the-insert-into-dbo-blah-part), it may help. – linuxartisan Apr 10 '18 at 08:15
  • @linuxartisan thanks but the answer by noufalcep did the trick for me – Muzammil Baloch Apr 10 '18 at 08:17
  • @linuxartisan you may want to check https://laravel.com/docs/5.3/queries#inserts this if you want to insert multiple records in laravel – Muzammil Baloch Apr 10 '18 at 08:18

1 Answers1

0

Do the loop before insert query

$array = array();

for ($i=0; $i <count($memberships) ; $i++){
    $array[] = array(
        'membership_id' => $memberships[$i],
        'masterdealer_id' => $dealer,
        'dealer_id' => null,
        'user' => 'masterdealer',
        'operation' => 'Accessible',
        'admin_id' => $data['id'],
        'created_at' => $now->format('Y-m-d H:i:s'),
        'updated_at' => $now->format('Y-m-d H:i:s')
    );
}

DB::table('accessible_membership_logs')->insert($array);
noufalcep
  • 3,446
  • 15
  • 33
  • 51