skip to Main Content

I am currently facing issues with an ajax search, I think it has to do with the routes, but I’m not sure what is wrong. Maybe it’s too early for coding.

This works

<script type="text/javascript">
$('#name').on('keyup',function(){
    $value=$(this).val();
    $.ajax({
        type : 'get',
        url : '{{ URL::to("/search") }}',
        data:{'name':$value},
        success:function(data){
            $('tbody').html(data);
        }
    });
})
</script>

This does not work

<script type="text/javascript">
$('#name').on('keyup',function(){
    $value=$(this).val();
    $.ajax({
        type : 'get',
        url : '{{ URL::to("/associates/search") }}', // Does not change if route name is used
        data:{'name':$value},
        success:function(data){
            $('tbody').html(data);
        }
    });
})
</script>

The routes are the following

Route::resource('associates', 'AssociateController');    

Route::get('/search','AssociateController@search');

Route::get('/associates/search', 'AssociateController@search')->name('associate.search');

The search method in the controller

public function search(Request $request)
{   
    if($request->ajax())
    {
        $output="";
        $associates=DB::table('associates')->where('name','LIKE','%'.$request->name.'%')->limit(10)->get();
        if($associates)
        {
            foreach ($associates as $key => $associate) {
                $output.='<tr>'.
                '<td>'.$associate->id.'</td>'.
                '<td>'.$associate->name.'</td>'.
                '</tr>';
            }
            return Response($output);
        }
    }
}

2

Answers


  1. If you’re already using named routes ->name('associate.search'), try to get the route URL by its name:

    url: '{{ route("associate.search") }}',
    
    Login or Signup to reply.
  2. The route URI /associates/search conflict with resource URI.

    Why?

    Because you created a resource route with URI associates :

    Route::resource('associates', 'AssociateController');
    

    This resource is reserved for :

    Verb          Path                             Action  Route Name
    GET           /associates                      index   associates.index
    GET           /associates/create               create  associates.create
    POST          /associates                      store   associates.store
    GET           /associates/{associates}         show    associates.show
    GET           /associates/{associates}/edit    edit    associates.edit
    PUT|PATCH     /associates/{associates}         update  associates.update
    DELETE        /associates/{associates}         destroy associates.destroy
    

    That’s why /associates/search conflict with that resource URI.

    How to solve?

    You need , just add a route to that method separately, before you register the resource:

    Route::get('/associates/search', 'AssociateController@search')->name('associate.search');
    Route::resource('associates', 'AssociateController'); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search