通过ajax从刀片中的控制器附加表数据

My table is designed for the controller, I want to append this table to the table in the blade of laravel.

This is my table in controller:

        $subLaws='';
          $n=1;
          foreach($sublaw as $sLaw)
          {
             $govt_no='';
             $eventType='';
             $status='Inactive';
             $url=url('admin/sublawmaster/'.$sLaw->id);
             $lmsid=$sLaw->lms_id;
             $view_url=url('admin/sublawmaster/view/' .$sLaw->id);
             if($sLaw->status=='1'){$status='Active';}
             $subLaws .= '<tr class="odd">
                          <td></td>
                          <td><a href="'.$view_url.'">'.$lmsid.'</a></td>
                          <td>'.$sLaw->sub_law_name.'</td>
                          <td>'.$eventType.'</td>
                          <td><span class="label label-success 
 btn_status">'.$status.'</span></td>
           $n++;
    }
           return json_encode($subLaws);

this is my ajax code:

$.ajax({
                type: 'GET',
                dataType: "json",
                data: {law_id:id},
                url: "{{ URL::to('admin/postlawid/') }}",
                success: function (data) {

                }
            });

this is my table in blade file:

<table class="table table-striped table-bordered table-hover" id="sublawmasterdata">
<thead>
            <tr id="header">
            <th align="center" width="80" style="text-align:center;">S NO</th>
                 <th>LMS ID</th>
                 <th>LMS Name</th>
                 <th>LMS Type</th>
                 <th>Status</th>
                 <th>Action</th>
             </tr>
             </thead>
             <tbody>

             </tbody>
            </table>

I have a dropdown when the value is selected from this dropdown the table in the controller should get appended to the table in blade file. how should I append the table in the controller to the table in blade file?

You may replace table content like as following.

$.ajax({
     type: 'GET',
     dataType: "json",
     data: {law_id:id},
     url: "{{ URL::to('admin/postlawid/') }}",
     success: function (data) {
         $("#sublawmasterdata tbody").innerHtml(data.table_html);
     }
});

If the data you are returning is plain html then there is no reason to json encode it. simply return the data

controller:

return $subLaws;

ajax:

$.ajax({
    type: 'GET',
    dataType: "html",
    data: {law_id:id},
    url: "{{ URL::to('admin/postlawid/') }}",
    success: function (data) {
        $('table#sublawmasterdata tbody').append(data);
    }
});

You could take it a step further and place the html code in a blade view and return that. But honestly I prefer to just return a json object with the necessary data and build the html from javascript

Try this:

$.ajax({
      type: 'GET', 
      dataType: "json", 
      data: {law_id:id},
      url: "{{ URL::to('admin/postlawid/') }}", 
      success: function (data) { 
            $("#sublawmasterdata tbody").innerHtml(data.table_html);
      } 
});

In your controller need to make change as given below

return json_encode(['table_html' => $subLaws]);

Use following ajax code to append rows dynamically

$.ajax({
      type: 'GET',
      dataType: "json",
      data: {law_id:id},
      url: "{{ your route to controller function }}",
      success: function (data) {
          ("#sublawmasterdata tbody").innerHtml(data.table_html); }
      }
});