0

Want to Access Show function data through AJAX, but it returns error when i passed id variable in route

Contoller

public function show($id)
{
    $features['UnitFeatures'] = UnitFeatures::find($id);
    return $features;
}

View Blade File

$(document).ready(function(){
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

var feature_id = $('#unitFeatures').val();

$.ajax({
    url: '{{ route('unitfeatures.show',feature_id) }}', // error in this line when i pass feature_id value to route
    type: 'GET',
    dataType: 'json',
    success: function(response){
      console.log(response);
     }
    });
  });

Please give me a solution

MUSTUFA
  • 25
  • 6

2 Answers2

2

The problem is that you cannot access a JS variable in your {{}} PHP code. You will have to get the route uri and replace the placeholder manually in your JS code.

You can get the URI of your Route with this piece of code: \Route::getRoutes()->getByName('unitfeatures.show ')->uri

This returns the uri as a string like that: /sometext/{id}

Now you can simply replace the {id} with the feature_id by using str.replace() or whatever function you like.

var feature_id = $('#unitFeatures').val();
var origUrl = '{{ \Route::getRoutes()->getByName('unitfeatures.show ')->uri}}';
$.ajax({
    url: origUrl.replace('{id}', feature_id), 
    type: 'GET',
    dataType: 'json',
    success: function(response){
      console.log(response);
     }
    });
  });
Aless55
  • 2,652
  • 2
  • 15
  • 26
  • i will use route url also. but the issue is that i use Route::resource('unitfeatures','FeaturesController') so it auto generates SHOW function. That i call unitfeatures.show – MUSTUFA Aug 18 '20 at 09:32
  • I know the problem. I did not do any research on it, but maybe there is a way so you can access the raw named route. – Aless55 Aug 18 '20 at 09:35
  • @MUSTUFA did it work or did you find another answer? – Aless55 Aug 19 '20 at 09:29
0

Problem With You are using Javascript Variable in PHP Code You can use Javascript Variable After Execution of PHP Code treated as a String.

$.ajax({
    url: "{{ route('unitfeatures.show') }}"+'/'+feature_id,
    type: 'GET',
    dataType: 'json',
    success: function(response){
      console.log(response);
     }
    });
  });
Mayank Rai
  • 49
  • 8
  • this won't work because the `route('unitfeatures.show', $id)` needs the $id to generate the route – Aless55 Aug 18 '20 at 11:41
  • @Aless55 `route('unitfeatures.show', $id)` will generate URL same as `http://127.0.0.1:8000/your_url/id` show you can make it the same way I answered, I have tested its working fine. – Mayank Rai Aug 19 '20 at 08:16
  • yes, but this `{{ route('unitfeatures.show') }}` will give you an error – Aless55 Aug 19 '20 at 09:06
  • you need to define it in route file `Route::get('/your_url', 'controller@function')->name('unitfeatures.show');` then `{{ route('unitfeatures.show') }}` will work fine. – Mayank Rai Aug 19 '20 at 09:11
  • @Aless55 please check the laravel docs for **Generating URLs To Named Routes** – Mayank Rai Aug 19 '20 at 09:13
  • the route is defined as: `Route::get('/your_url/{id}', 'controller@function')->name('unitfeatures.show');` , so the `$id` is needed for calling the route, you cannot call it without it. – Aless55 Aug 19 '20 at 09:18
  • you can add it manually after generating the route string, concatenate the URL string with id which you are getting in javascript variable like I have done in the above answer. – Mayank Rai Aug 19 '20 at 09:26
  • it is only possible if your route is defined without the $id parameter, otherwise calling your named route will throw the following error: `Illuminate\Routing\Exceptions\UrlGenerationException : Missing required parameters for [Route:....` – Aless55 Aug 19 '20 at 09:28
  • yes you need to remove `{id}` from your route otherwise it will throw an error. – Mayank Rai Aug 19 '20 at 10:05