Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

baloch's avatar

Query builder - inside insert query i want to implement a for loop over the array to insert multiple records in one go

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($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); 
            }
        }```
0 likes
4 replies
ekhlas's avatar
$insertarr='';
 for ($i=0; $i <count($memberships) ; $i++){
                       $insertarr[]=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([$insertarr]);
baloch's avatar

@ekhlas this is the exception it's showing now

Assignments can only happen to writable values

in adminController.php (line 1236)```
ekhlas's avatar

give me your database table detail and model code if you created model.

look like there is something missing table creation or in model code

baloch's avatar
baloch
OP
Best Answer
Level 1

@ekhlas i found the solution, i hope it helps someone else too


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);


Please or to participate in this conversation.