Heng Sopheak Heng Sopheak - 15 days ago 4
PHP Question

How to save multiple rows to database using for loop

I'm try to save all those data to DB by using for loop and addr_types have already equal to more than 1. but I got only one row in DB.
It should be insert more than one as amount of addr_types but it insert only one row.



use App\Models\MyModels;
use DB;

class ClientCbcAddress extends MyModels
{

protected $table = 'address';
//protected $fillable = ['role_id','name', 'email','username'];
protected $hidden = ['created_at', 'updated_at'];
public $timestamps = false;

private $Add_rules = [
'country_id' => 'required',
'province_Id' => 'required',
'id_number_1' => 'required',
'address_en' => 'required',
];

public function SaveClientAddress($data, $id)
{
if (!$this->Check_validator($data, $this->Add_rules)){
return $this->Check_validator($data, $this->Add_rules);
}

for ($i = 0; $i <= count($data['addr_types']) - 1; $i++) {

$city_code = '';
$postal_code = '';

if (trim($data['provinces']) === 'PNH' && trim($data['country']) === 'KHM') {
$city_code = 'PNH';
$postal_code = $data['provinces'][$i];
}

$identification = new self();
$identification->client_id = (int)$id;
$identification->country_id = (int)$data['country'][$i];
$identification->province_Id = (int)$data['provinces'][$i];
$identification->district_id = (int)$data['district'][$i];
$identification->commune_id = (int)$data['commune'][$i];
$identification->village_id = (int)$data['villages'][$i];

$identification->address_type = $data['addr_types'][$i];
$identification->address_en = $data['address_en'][$i];
$identification->address_kh = $data['address_kh'][$i];
$identification->city_code = $city_code;
$identification->postal_code = $postal_code;

if($identification->save()){
return $identification->attributes['id'];
}
}
}
}


DB

This is Table option

Answer

Try this:

    use App\Models\MyModels;
    use DB;

    class ClientCbcAddress extends MyModels
    {

            protected $table = 'address';
            //protected $fillable = ['role_id','name', 'email','username'];
            protected $hidden = ['created_at', 'updated_at'];
            public $timestamps = false;

            private $Add_rules = [
                'country_id' => 'required',
                'province_Id' => 'required',
                'id_number_1' => 'required',
                'address_en' => 'required',
            ];

            public function SaveClientAddress($data, $id)
            {
                if (!$this->Check_validator($data, $this->Add_rules)){
                    return $this->Check_validator($data, $this->Add_rules);
                }

                $array_ids = array();
                for ($i = 0; $i <= count($data['addr_types']) - 1; $i++) {

                    $city_code = '';
                    $postal_code = '';

                    if (trim($data['provinces']) === 'PNH' && trim($data['country']) === 'KHM') {
                        $city_code = 'PNH';
                        $postal_code = $data['provinces'][$i];
                    }

                    $identification = new self();
                    $identification->client_id = (int)$id;
                    $identification->country_id = (int)$data['country'][$i];
                    $identification->province_Id = (int)$data['provinces'][$i];
                    $identification->district_id = (int)$data['district'][$i];
                    $identification->commune_id = (int)$data['commune'][$i];
                    $identification->village_id = (int)$data['villages'][$i];

                    $identification->address_type = $data['addr_types'][$i];
                    $identification->address_en = $data['address_en'][$i];
                    $identification->address_kh = $data['address_kh'][$i];
                    $identification->city_code = $city_code; 
                    $identification->postal_code = $postal_code; 

                    if($identification->save()){
                      $array_ids[$i] = $identification->attributes['id'];
                    }
                }

                return $array_ids;
            }
    }