Ads 4 You

Dependent dropdown for country, state, city and area in yii2

Email Id : phpmk888@gmail.com

Dependent dropdown for country, state, city and area in Yii2

I had created two database tables as,
1. Country
2. State
3. City
4. Area

1Setp : Create a database table area_master

 


CREATE TABLE `area_master` (
  `id` int(11) NOT NULL,
  `country_id` int(11) DEFAULT NULL,
  `state_id` int(11) DEFAULT NULL,
  `city_id` int(11) NOT NULL,
  `area_name` varchar(150) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `city_master` (
  `id` int(11) NOT NULL,
  `country_id` int(11) DEFAULT NULL,
  `state_id` int(11) NOT NULL,
  `city_name` varchar(150) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `country_master` (
  `id` int(11) NOT NULL,
  `sortname` varchar(3) NOT NULL,
  `name` varchar(150) NOT NULL,
  `phonecode` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='country_master';

CREATE TABLE `state_master` (
  `id` int(11) NOT NULL,
  `name` varchar(30) NOT NULL,
  `country_id` int(11) NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `area_master`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `city_master`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `country_master`
  ADD PRIMARY KEY (`id`),
  ADD KEY `id` (`id`),
  ADD KEY `phonecode` (`phonecode`);

ALTER TABLE `state_master`
  ADD PRIMARY KEY (`id`),
  ADD KEY `country_id` (`country_id`);

ALTER TABLE `area_master`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=0;

ALTER TABLE `city_master`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=0;

ALTER TABLE `country_master`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=0;

ALTER TABLE `state_master`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=0;




2setp : got gii and Model Generator
Add Table Name : and Generate model

3step : go to gii and select Module Generator

4step : add Modules in Web.config

'modules' => [       
        'area' => ['class' => 'app\modules\area\Admin',],       
    ],

5step : go to gii and select CRUD Generator



6step : Form Create State,City Dependent Dropdown.

7step : join Relation in Model

8step : Get City,State and country name in Grid view


Yii2 Application Development Country, State, City and Area CRUD Master.

Comments

  1. Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!
    Hire Remote Yii Framework Developers in India

    ReplyDelete

Post a Comment