Eager loading with DataTables plugin

Eager loading with DataTables plugin

dmotorsdmotors Posts: 6Questions: 2Answers: 0
edited October 2015 in Free community support

Currently I am using jQuery DataTables plugin in which I send a JSON response to the view in which the jQuery picks it up and displays the table accordingly. This works fine but I'm kind of stuck at the part where I'm eager loading hasOne relationship and not sure how to display it properly once in view.

Controller:

public function anyData() {
    return Datatables::of(My_Item_Table::with('sixmonth')->get())->make(true);
}

My_Item_Table Model:

public function sixmonth()
{
    return $this->hasOne('App\models\DB\My_Item_Sold', 'sku');
}

This is what I would do without datatables:

<table id="itemstable" class="table table-striped table-bordered tablesorter table-fixed-header" data-fixed-header-top-offset="100" data-fixed-header-bg-color="white">
    <thead class="fixed-header">
        <tr>
            <th>SKU</th>
            <th>6 Month</th>
        </tr>
    </thead>
    <tbody>
    foreach($items as $k => $v)
        <tr>
            <td>{{ $v->sku }}</td>
            <td>{{ $v->sixmonth->lastOrderDate }}</td>
        </tr>
    endforeach
    </tbody>
</table>   

Does anyone know how to achieve this using datatables?

(btw I'm using Laravel but I think once I get the general answer I should be able to figure it out from there)

Answers

  • dmotorsdmotors Posts: 6Questions: 2Answers: 0

    Nevermind from trial and error figured it out myself. Under "columnDefs" in the jQuery I put:

            {
              targets: 5,
              render: function ( data, type, full ) {
                return full.sixmonth.lastOrderDate;
              }
            },
    
  • allanallan Posts: 63,755Questions: 1Answers: 10,509 Site admin

    Good to hear you got it sorted out.

    Allan

This discussion has been closed.