Hey all,
I'm trying to create a dynamic select in an HTML form by way of AJAX.
Here's the javascript from the HTML page...
<script>
function fetch_select(val)
{
$.ajax({
type: 'post',
url: '/update_model_select',
data: {
manufacturer_id:val
},
dataType: "json",
// success: function (data) {
// console.log(data);
// }
success: function (data) {
document.getElementById("model").innerHTML="<option value=\"" + data.id + "\">" + data.model + "</option>";
}
});
}
</script>
My route...
Route::post('update_model_select', 'ComponentsController@update_model_select');
ComponentsController...
public function update_model_select(Request $request) {
$manufacturer = Manufacturer::find($request->manufacturer_id);
$components = $manufacturer->components->pluck('id', 'model');
return $components->toJson();
}
Problem is that I'm not getting the JSON format I was expecting. Here's a sample of the data output from the console...
{VLF-250: 6, VLF-500: 7, VLC-RO: 8}
VLC-RO
:
8
VLF-250
:
6
VLF-500
:
7
I'm not sure how to iterate this out without they keys. Is it possible to return the keys as well?