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

6
  • Does this answer your question? stackoverflow.com/questions/29308441/… Commented Aug 18, 2020 at 9:23
  • No, i get a id from a input value and pass in route Commented Aug 18, 2020 at 9:29
  • 1
    Maybe this then: stackoverflow.com/questions/27634285/… Commented Aug 18, 2020 at 9:54
  • Try passing id in array with key value pair, Something like this route('unitfeatures.show', ['id' => feature_id]) Commented Aug 18, 2020 at 10:30
  • 1
    @LQS that will not work because you cannot insert a JS variable into PHP code Commented Aug 18, 2020 at 11:22

2 Answers 2

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);
     }
    });
  });
Sign up to request clarification or add additional context in comments.

3 Comments

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
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.
@MUSTUFA did it work or did you find another answer?
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);
     }
    });
  });

9 Comments

this won't work because the route('unitfeatures.show', $id) needs the $id to generate the route
@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.
yes, but this {{ route('unitfeatures.show') }} will give you an error
you need to define it in route file Route::get('/your_url', 'controller@function')->name('unitfeatures.show'); then {{ route('unitfeatures.show') }} will work fine.
@Aless55 please check the laravel docs for Generating URLs To Named Routes
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.