0

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

12
  • But what kind of error do you get? Commented Nov 25, 2016 at 7:47
  • There is no errors but it has inserted only one rows Commented Nov 25, 2016 at 7:48
  • Do you have any UNIQUE keys? Commented Nov 25, 2016 at 7:48
  • Anyway why did you place SaveClientAddress function into your model and not into a controller? Commented Nov 25, 2016 at 7:51
  • 1
    I think you are doing return $identification->attributes['id']; after first save from the loop so you are getting only one record in DB. Commented Nov 25, 2016 at 7:55

1 Answer 1

1

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;
            }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.