如何使用for循环将多行保存到数据库

时间:2016-11-25 07:41:20

标签: php laravel for-loop

我尝试使用for循环将所有这些数据保存到DB,而 addr_types 已经等于大于1.但我在DB中只有一行。 它应该插入多个addr_types,但它只插入一行。

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

1 个答案:

答案 0 :(得分:1)

试试这个:

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