lundi 3 février 2020

Having problems to pass searched data from controller blade file using ajax in laravel

This is my controller method in Admin Controller , this method receive the search input but i am facing problems to pass output data to the view file

public function action(Request $request)
{
    if($request->ajax())
    {

        $students=Student::where('name','LIKE','%'.$request->search."%")->paginate(5);

        $data = $students->count();
        if($data > 0)
        {
            $output=$students;

        }
        else{
           $output=$students;
        }
        return view('search' compact('output'));

    }

}

Here is the ajax in view file (search.blade.php)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">

    // $(document).on('keyup', '#search', function(){
    $('#search').on('keyup',function(){
        $value=$(this).val();
        $.ajax({
            type : 'get',
            url : '',
            data:{'search':$value},
            success:function(data){
                $('tbody').html(data);
            }
        });

    })

</script>

<script type="text/javascript">
    $.ajaxSetup({ headers: { 'csrftoken' : '' } });
</script>

Here is the ajax route

Route::get('/search/action', 'AdminController@action')->name('search.action');


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire