在 laravel 中使用 Ajax 显示数据

display data using Ajax in laravel

我正在尝试从 table 'Company' 的数据库中获取数据,输出结果为 json 格式,我必须将其显示在数据中Table.

Controller

public function ex(Request $request){
    $table =  \DB::table('company')->select('name','type','address','city','email','description')->get();
    //return view('mydata')->with('company',$table);
    return response($table);
}

Route

Route::get('display','Test@ex');

Blade page

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        $("button").click(function(){
           $("table").toggle();
           $.get("{{URL::to('display')}}",function (data) {
               $.each(data,function (i,value) {
                 var  tr =$("<tr/>");
                 tr.append($("<td/>",{
                     text : value.name /*Get first column*/
                 }))
               })
           })
        });
    });
    </script> 
</head> 

<body> 
<button>Click Me</button> 
<table border="1px" class="table table-striped table-bordered" style="width:100%">
    <thead>
    <tr>
        <th>Name</th>
        <th>Type</th>
        <th>Address</th>
        <th>City</th>
        <th>Email</th>
        <th>Description</th>
    </tr>
    </thead> 
    </table> 
</body> 
</html>

输出如下图所示,但我想在数据中显示它table

您可以使用 yajjra laravel 包通过 ajax

显示数据

这里是通过 ajax 在数据表中显示数据的完整教程 Datable Tutorial