I am using Laravel 5.4, I have three forms (Create - Edit - Clone) and in the 3 I need to execute the following Ajax request, for this reason the url of each form varies:
http://app.dev/users/datasheet/create
http://app.dev/users/datasheet/6/edit
http://app.dev/users/datasheet/6/clone
Route.php:
Route::get('datasheet/create/machine_brand_list/{option?}', 'Datasheet\DatasheetController@getMachineBrandList');
Driver.php:
public function getMachineBrandList(Request $request, $option){
if($option == 'Rollo' && $request->ajax()){
$id = auth()->id();
$machinesBrand = Machine::where('user_id','=',$id)->get();
return response()->json($machinesBrand);
}
}
Ajax:
function infoMachinesBrand(){
var selectValor = this.value;
var url = "create/machine_brand_list/"+selectValor;
//var url = "edit/machine_brand_list/"+selectValor;
//var url = "clone/machine_brand_list/"+selectValor;
if (selectValor == 'Rollo') {
//$("#div_machine_converter_select").show();
$('#machine_type').prop("disabled", false);
$.get(url, function(data){
$('#machine_type').append('<option value="">Seleccione</option>');
$.each(data,function(index, value){
$('#machine_type').append('<option value="'+value.id+'">'+value.dsc_name+'</option>');
});
}).fail(function(jqXHR, textStatus, errorThrown){
console.log(jqXHR,textStatus,errorThrown);
});
} else {
//$("#div_machine_converter_select").hide();
$('#machine_type').prop("disabled", true);
$('#machine_type').empty()
clear = document.getElementById('nu_useful_width_min').value = "";
clear = document.getElementById('nu_useful_width_max').value = "";
clear = document.getElementById('nu_core_diameter').value = "";
clear = document.getElementById('nu_weight_supported_max').value = "";
}
}
$('#product_type').change(infoMachinesBrand);
In the exposed Ajax code it is seen that in the URL variable I have to prefix the last part of the url for the Ajax request to work, but I need to not depend on it since there are three forms where I must execute the same function , in this case, how can I create a route in Laravel that allows me to execute this function, regardless of the URL in which I am? Is this possible?