Search code examples
laravellaravel-routinglaravel-7

Laravel Routes - Possible to reuse ajax route call from one form on different form?


I have an existing route I created to perform an ajax call for a chained select:

Route::get('add/{id}','AddAssetController@getModel');

The above queries the db for models belonging to each manufacturer.

The url part of the ajax call (man_ID is the id of the manufacturer from the select):

url: 'add/' + man_ID,

The above works perfectly on the main form where I am using it. However, I've found that I need this chained select on more than one form. Is there a way to be able to use the same route for two different pages? I've tried calling it from a different page and in the console, I'm getting a 404 error.

Am I missing something? Is this possible?


Solution

  • You are probably calling the script from the differently nested pages, thus add/ is ultimately calling different path. I would strongly advise you name your routes, and generate links like that:

    Route::get('add/{id}','AddAssetController@getModel')->name('get-model');
    

    and in your blade file

    url: '{{route('get-model', ['id' => $id])}}',
    

    if you still set on using paths to call your script, use them relative to your root:

    url: '/add/' + man_ID,