Prathamesh Doke
Prathamesh Doke

Reputation: 845

display data using Ajax in laravel

I'm trying to fetch the data from the database from a table 'Company' in the output it coming as a json format,i have to display it in a Data 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>

the output coming as in following image but i want to display it in a Datatable

output in JSON format

Upvotes: 0

Views: 4277

Answers (1)

you can use yajjra laravel package to show data via ajax

here is the complete tutorial to show data via ajax in datatable Datable Tutorial

Upvotes: 1

Related Questions