Eager loading with DataTables plugin
Eager loading with DataTables plugin
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
Nevermind from trial and error figured it out myself. Under "columnDefs" in the jQuery I put:
Good to hear you got it sorted out.
Allan