-
-
Save johnef/b2a0a7f4d3c4d35ad54a7d95eb57fef8 to your computer and use it in GitHub Desktop.
Dynamic dropdown in Laravel, let's say you have multiple drop downs (selects), you click on one and the contents of the other need to be dynamically changed. One solution is to dynamically load JSON from the API and change the dropdown dynamically depending on the user choice.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE `makers` ( | |
`id` int(10) unsigned NOT NULL, | |
`name` varchar(255) NOT NULL, | |
`description` varchar(255) NOT NULL, | |
`created_at` datetime NOT NULL, | |
`updated_at` datetime NOT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB DEFAULT CHARSET=latin1; | |
-- | |
-- Dumping data for table `makers` | |
-- | |
INSERT INTO `makers` (`id`, `name`, `description`, `created_at`, `updated_at`) VALUES | |
(1, 'Toyota', 'Toyota cars', '2013-03-11 00:00:00', '2013-03-11 00:00:00'), | |
(2, 'Honda', 'Honda cars', '2013-03-11 00:00:00', '2013-03-11 00:00:00'), | |
(3, 'Mercedes', 'Mercedes cars', '2013-03-11 00:00:00', '2013-03-11 00:00:00'); | |
-- -------------------------------------------------------- | |
-- | |
-- Table structure for table `models` | |
-- | |
CREATE TABLE `models` ( | |
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, | |
`maker_id` int(10) unsigned NOT NULL, | |
`name` varchar(255) NOT NULL, | |
`description` varchar(255) NOT NULL, | |
`created_at` datetime NOT NULL, | |
`updated_at` datetime NOT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ; | |
INSERT INTO `models` (`id`, `maker_id`, `name`, `description`, `created_at`, `updated_at`) VALUES | |
(1, 2, 'Honda S2000', '', '2013-03-11 22:32:07', '2013-03-11 22:32:07'), | |
(2, 2, 'Civic', '', '2013-03-11 22:32:46', '2013-03-11 22:32:46'), | |
(3, 2, 'Fit', '', '2013-03-11 22:34:35', '2013-03-11 22:34:35'), | |
(4, 1, 'asdf asdf', '', '2013-03-11 22:35:31', '2013-03-11 22:35:31'), | |
(5, 1, 'Yaris', '', '2013-03-11 22:36:01', '2013-03-11 22:36:01'), | |
(6, 1, 'Corolla', '', '2013-03-11 22:36:23', '2013-03-11 22:36:23'), | |
(7, 1, 'Camry', '', '2013-03-11 22:36:31', '2013-03-11 22:36:31'), | |
(8, 3, 'SLK 500', '', '2013-03-11 22:36:47', '2013-03-11 22:36:47'), | |
(9, 3, 'C300', '', '2013-03-11 22:36:50', '2013-03-11 22:36:50'), | |
(10, 2, 'another item', '', '2013-03-11 22:36:52', '2013-03-11 22:36:52'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Maker extends Eloquent { | |
public function models(){ | |
return $this->has_many('Model'); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Model extends Eloquent { | |
public function maker(){ | |
return $this->belongs_to('Maker'); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Route::get('api/dropdown', function(){ | |
$input = Input::get('option'); | |
$maker = Maker::find($input); | |
$models = $maker->models(); | |
return Response::eloquent($models->get(['id','name'])); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function($){ | |
$('#make').change(function(){ | |
$.get("{{ url('api/dropdown')}}", | |
{ option: $(this).val() }, | |
function(data) { | |
var model = $('#model'); | |
model.empty(); | |
$.each(data, function(index, element) { | |
model.append("<option value='"+ element.id +"'>" + element.name + "</option>"); | |
}); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Dropdown demo</h1> | |
{{ Form::open() }} | |
<select id="make" name="make"> | |
<option>Select Car Make</option> | |
<option value="1">Toyota</option> | |
<option value="2">Honda</option> | |
<option value="3">Mercedes</option> | |
</select> | |
<br> | |
<select id="model" name="model"> | |
<option>Please choose car make first</option> | |
</select> | |
{{ Form::close();}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment