0

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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.