Home > Mobile >  Foreach loop not working properly in my Laravel 9 Code?
Foreach loop not working properly in my Laravel 9 Code?

Time:12-18

Screen Shot of HTML Form. Can you suggest me where am I doing mistakes in my Laravel app, each time i am uploading multiple files throug the form, files is uploading perfectly in given location, but only 1 records are being inserted into database table. Here is my code...

// Upload bg_certificateArr photo
if ($request->hasFile('bg_certificate')) {
    foreach ($request->file('bg_certificate') as $key => $file) {
        // Get image extention
        $extention =  $file->getClientOriginalExtension();
        // Generate new image name
        $bgCertImgName = 'ac-bg-cert-' . date('Y-m-d-H-i-s') . '.' . $extention;
        $bgCertImgPath = 'accountant/images/bank_guarantee/' . $bgCertImgName;
        // Upload Profile Image
        Image::make($file)->save($bgCertImgPath);
        // Insert data into bank guarantee table
        $bg_amountArr = $data['bg_amount'];
        $bg_numberArr = $data['bg_number'];
        $bank_idArr = $data['bank_id'];
        $bg_from_dateArr = $data['bg_from_date'];
        $bg_to_dateArr = $data['bg_to_date'];
        $bg_amount = $bg_amountArr[$key];
        $bg_number = $bg_numberArr[$key];
        $bank_id = $bank_idArr[$key];
        $bg_from_date = $bg_from_dateArr[$key];
        $bg_to_date = $bg_to_dateArr[$key];
        // echo '<pre>';
        // print_r($bg_to_date);
        // die();
        $ac_bg->school_id = $schoolID;
        $ac_bg->ledgers_id = $acLedger->id;
        $ac_bg->bg_amount = $bg_amount;
        $ac_bg->bg_number = $bg_number;
        $ac_bg->bank_id = $bank_id;
        $ac_bg->bg_from_date = $bg_from_date;
        $ac_bg->bg_to_date = $bg_to_date;
        $ac_bg->bg_certificate = $bgCertImgName;
        $ac_bg->save();
    }
}

CodePudding user response:

You haven't posted full code here, but I believe before loop you somehow set $ac_bg variable. So looking at your code you create one record and update it in every next loop iteration.

So to fix this you should probably do something like this:

 foreach ($request->file('bg_certificate') as $key => $file) {
   $ac = new Ac(); // don't know what's the exact model class here

Then in every loop iteration you will create new record instead of updating it.

CodePudding user response:

initialize $ac_bg for every foreach loop.

if ($request->hasFile('bg_certificate')) {
    foreach ($request->file('bg_certificate') as $key => $file) {

        $ac_bg = new AccountBankGuarantee; // like this

        // Get image extention
        $extention =  $file->getClientOriginalExtension();
        // Generate new image name
        $bgCertImgName = 'ac-bg-cert-' . date('Y-m-d-H-i-s') . '.' . $extention;
        $bgCertImgPath = 'accountant/images/bank_guarantee/' . $bgCertImgName;
        // Upload Profile Image
        Image::make($file)->save($bgCertImgPath);
        // Insert data into bank guarantee table
        $bg_amountArr = $data['bg_amount'];
        $bg_numberArr = $data['bg_number'];
        $bank_idArr = $data['bank_id'];
        $bg_from_dateArr = $data['bg_from_date'];
        $bg_to_dateArr = $data['bg_to_date'];
        $bg_amount = $bg_amountArr[$key];
        $bg_number = $bg_numberArr[$key];
        $bank_id = $bank_idArr[$key];
        $bg_from_date = $bg_from_dateArr[$key];
        $bg_to_date = $bg_to_dateArr[$key];
        // echo '<pre>';
        // print_r($bg_to_date);
        // die();
        $ac_bg->school_id = $schoolID;
        $ac_bg->ledgers_id = $acLedger->id;
        $ac_bg->bg_amount = $bg_amount;
        $ac_bg->bg_number = $bg_number;
        $ac_bg->bank_id = $bank_id;
        $ac_bg->bg_from_date = $bg_from_date;
        $ac_bg->bg_to_date = $bg_to_date;
        $ac_bg->bg_certificate = $bgCertImgName;
        $ac_bg->save();
    }
}
  • Related