0

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 : '{{route('search.action')}}',
            data:{'search':$value},
            success:function(data){
                $('tbody').html(data);
            }
        });

    })

</script>

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

Here is the ajax route

Route::get('/search/action', 'AdminController@action')->name('search.action');
  • Did you get any errors? if yes please update. – user6016913 Feb 04 '20 at 08:24
  • No but the data is not being returned – Rumon Ahmed Feb 04 '20 at 19:53
  • `view()` does not return HTML - you need to `render()` it. There are quite a few questions about this here on SO. The more usual and probably better approach is to have your controller return *data* - eg JSON - and have your front end render that, eg iterate over an array of results and create HTML to insert in the DOM. – Don't Panic Feb 05 '20 at 09:45
  • Does this answer your question? [How can I return a view from an AJAX call in Laravel 5?](https://stackoverflow.com/questions/28634712/how-can-i-return-a-view-from-an-ajax-call-in-laravel-5) – Don't Panic Feb 05 '20 at 09:45

1 Answers1

0

Update the below source code.

AdminController.php

public function action(Request $request)
    {
        if ($request->ajax()) {
            // Convert to lowercase after trim the searched text
            $search_text = strtolower(trim($request->search));
            $students = Student::where('name','LIKE','%'.$search_text."%")->paginate(5);
            $data = $students->count();
            $output = array();
            if ($data > 0) {
                $output = $students->toArray();
            }
            // Return JSON response
            return response()->json($output);
        }
    }

search.blade.php

<input type="text" id="search">

<div id="studentlist">
    <table>
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
            </tr>
        </thead>
        <tbody>            
        </tbody>
    </table>
</div>

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

    $('#search').on('keyup',function(){
        $value=$(this).val();
        $.ajax({
            type : 'get',
            url : '{{route('search.action')}}',
            data:{'search':$value},
            success:function(response){
                var studentListHtml = "";
                if (response) {
                    if (response.data && response.data.length > 0) {
                        $.each(response.data , function( index, value ) {
                            studentListHtml += "<tr><td>" + value.id + "</td>" + "<td>" + value.name + "</td></tr>"; 
                        });
                        $("#studentlist table tbody").empty().html(studentListHtml);
                    } else {
                        $("#studentlist table tbody").empty().html("<tr><td colspan='2'>No students found</td></tr>");
                    }
                }
            }
        });

    })

</script>

<script type="text/javascript">
    $.ajaxSetup({ headers: { 'csrftoken' : '{{ csrf_token() }}' } });
</script>
user6016913
  • 181
  • 2
  • 10