Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenation between URL::action() and Javascript int variable

I need to do an AJAX request in laravel, and the url is a concatenation between tne URL::Action method and an Javascript integer variable.

The javascript function in the view is

function detailMaintenanceVehicle(vehicleId,placa){

    var url = "{{ URL::action('DetailMaintenanceController@getDetailMaintenance',["+vehicleId+"]) }}";

    console.log(vehicleId); // This prints integer variables 1,2,3..
    console.log(url); // This prints a string http://localhost/.../detailMaintenance/+vehicleId+

    $.ajax({
        url: url,
        dataType: 'json',
        type: 'GET',
        success: function(data) {
            ...
        },
        error: function(jqXHR, textStatus, errorThrown) {
            ...
        }
    });

}

The concatenation uses the variable name, it must use the value.

// Gives : http://localhost/.../detailMaintenance/+vehicleId+

// I need : http://localhost/.../detailMaintenance/3 (for example)

Please help me. Thanks in advance!.

like image 258
olavidps Avatar asked May 04 '26 01:05

olavidps


2 Answers

You can try as:

var url = "{{ URL::action('DetailMaintenanceController@getDetailMaintenance', ['vehicleId' => 'vehicleId']) }}";

url.replace("vehicleId", vehicleId);
like image 98
Amit Gupta Avatar answered May 07 '26 09:05

Amit Gupta


You should never generate JS with PHP. Create hidden input with data, for example:

{!! Form::hidden('url, action('DetailMaintenanceController@getDetailMaintenance)) !!}

If your action requires ID, just write partial URL manually.

And then get it in JS:

var url = $( "[name='url']" ) + vehicleId;
like image 26
Alexey Mezenin Avatar answered May 07 '26 09:05

Alexey Mezenin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!