Tuesday, April 10, 2018

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

inside laravel 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($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); 
            }
        }



from Laravel Questions and Answers https://laravelquestions.com/php/inside-laravel-insert-query-i-want-to-implement-a-for-loop-over-the-array-to-insert-multiple-records-in-one-go/
via Lzo Media

No comments:

Post a Comment