Problem Laravel Routes with Ajax

0

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?

    
asked by Darwin Gomez 22.12.2017 в 07:16
source

2 answers

1

Yes, a generic route can be generated, for the 3 cases:

Route::get('getMachineBrandList/{option?}', 'Datasheet\DatasheetController@getMachineBrandList');

And then, in the Ajax call, the corresponding url is set and ready:

function infoMachinesBrand(){

    var selectValor = this.value;
    var url = "getMachineBrandList/"+selectValor;

I understand that this Ajax is used to load the options of a Select (or drop-down list) after some event, so it should be independent of the urls that are used in the forms (creation, edition or cloning), to process later the data.

Greetings

    
answered by 05.01.2018 в 13:20
0

I think you have written the wrong route in ajax. On the one hand you want to run style routes:

http://app.dev/users/datasheet/create

In the routes file the route you have is:

datasheet/create/machine_brand_list/{option?}

And in Ajax, the one that exists is

create/machine_brand_list/"+selectValor

They seem very little. They must have the same structure, they must be the same

    
answered by 22.12.2017 в 08:36