Laravel与Ajax实时搜索Mysql数据库无结果

时间:2019-01-07 11:48:37

标签: mysql ajax laravel search live

您好,我正在使用ajax和Mysql在laravel中开发带有实时搜索的登录应用程序,但是当我尝试显示数据库表时,我一直得到空的tableview。经过一些调试后,我发现该程序在if($request->ajax())中停止,因此没有 在if($request->ajax())括号内进行交流。

有我的代码:

查看:

<body>
    <br />
    <div class="container box">
        <h3 align="center">Recherche de PDR</h3>
        <br /> @if(isset(Auth::user()->email))
        ....    
        <br />

        <div class="panel panel-default">
            <div class="panel-heading">Recherche de PDR</div>
            <div class="panel-body">
                <input type="text" name="search" id="search" class="form-control" placeholder="Recherche PDR" />
                <div class="table-responsive">
                    <h3 align="center">Total PDR : <span id="total_records"></span></h3>
                    <table class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th>Pièce</th>
                                <th>ID-Pièce</th>
                                <th>Status</th>
                            </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</body>

</html>

<script>
    $(document).ready(function(){

        fetch_pdr_data();

       function fetch_pdr_data(query = '')
       {
            $.ajax({
                url:"{{ route('recherche.action') }}",
                method:'GET',
                data:{query:query},
                dataType:'json'
                success:function(data)
                {
                    $('tbody').html(data.table_data);
                    $('#total_records').text(data.total_data);
                }
            })
       }

       $(document).on('keyup', '#search', function(){
            var query = $(this).val();
            fetch_pdr_data(query);
       });

    });

</script>

控制器:

<?php

    namespace App\Http\Controllers;
    use App\Pdrs;
    use DB;

    use Illuminate\Http\Request;

    class PDRController extends Controller
    {
        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function index()
        {
            return view('recherche.index');
        }

        function action(Request $request)
        {//working
            if($request->ajax())
            {//not working
                $query = $request->get('query');
                if($query != '')
                {
                    $data = DB::table('pdrs')
                            ->where('Désignation', 'like', '%'.$query.'%')
                            ->orWhere('Status', 'like', '%'.$query.'%')
                            ->get();
                }
                else
                {
                    $data = DB::table('pdrs')
                            ->orderBy('ID-Pièce', 'desc')
                            ->get();
                }
                $total_row = $data->count();
                if($total_row > 0)
                {
                    foreach($data as $row)
                    {
                        $output .= '
                        <tr>
                            <td>'.$row->Désignation.'</td>
                            <td>'.$row->ID-Pièce.'</td>
                            <td>'.$row->Status.'</td>
                        </tr>
                        ';
                    }
                }
                else
                {
                    $output = '
                    <tr>
                        <td align="center" colspan="5">No Data Found</td>
                    </tr>
                    ';
                }
                $data = array(
                    'table_data'        =>  $output,
                    'total_data'        =>  $total_data
                );

                echo json_encode($data);
            }
        }

这是我的web.php:

Route::get('/recherche', 'PDRController@index');
Route::get('/recherche/action', 'PDRController@action')->name('recherche.action');

0 个答案:

没有答案