skip to Main Content

I want to send data to the controller with ajax, but there is an error

Missing required parameters for `[Route: city] [URI: daftar / city / {id}].

here’s my ajax code

 $(".province").on("change",function(){
      var id = this.value;
      console.log(id);
      $.ajax({
        type: "get",
        url: "{{ route('city') }}"+'/'+id  ,
        dataType: "json",
        success: function(data){
            console.log('');
        },
        });
});

and this my route

Route::group(['prefix' => 'vendor'], function () {
 Route::get('/city/{id}', 'VendorVendorController@getCity')->name('city');
});

4

Answers


  1. You are using

    {{ route('city') }}
    

    without any parameter, so you need to change your route with this code:

    Route::group(['prefix' => 'vendor'], function () {
     Route::get('/city/{id?}', 'VendorVendorController@getCity')->name('city');
    });
    
    Login or Signup to reply.
  2. Try This.

    Route::group(['prefix' => 'vendor','as'=>'vendor.'], function () {
    Route::get('/city/{id}',['as' => 'activebranch', 'uses' => 'VendorVendorController@getCity']);
    });
    

    AjAX.

     $(".province").on("change",function(){
          var id = this.value;
          console.log(id);
          $.ajax({
            type: "get",
            url: "{{ route('vendor.activebranch') }}"+'/'+id  ,
            dataType: "json",
            success: function(data){
                console.log('');
            },
            });
    });
    
    Login or Signup to reply.
  3. you can’t write it this way. {{ route('city') }} is echoing the route which has a parameter. but the parameter is missing here. you are adding that parameter later using js but it won’t work as its missing when php is echoing the route. you can do it this way

    $(".province").on("change",function(){
        var id = this.value;
        var url = '{{ route("city", ":id") }}';
        url = url.replace(':id', id);
        $.ajax({
            type: "get",
            url: url,
            dataType: "json",
            success: function(data){
                console.log('');
            },
        });
    });
    
    Login or Signup to reply.
  4. You can’t use route(‘city’) without its parameter,

    If you want a simple way without the laravel helper you can try to change it like this :

    $(".province").on("change",function(){
          var id = this.value;
          console.log(id);
          $.ajax({
            type: "get",
            url: "daftar/city/" + id  ,
            dataType: "json",
            success: function(data){
                console.log('');
            },
            });
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search