1
Would you have some way to pass the model relationship to the columns of the Datatable server side?
For example in the Datatable script:
columns: [
{ data: 'marca_id', name: 'marca_id' },
{ data: 'automovel_modelo', name: 'automovel_modelo' },
{ data: 'action', name: 'action', orderable: false, searchable: false}
],
Where the tag_id would have to receive the relationship value to display the brand name according to the ID.
Function in the model with relationship:
public function marca(){
return $this->belongsTo('App\Models\Config\Transito\AutomovelMarca');
}
Controller that displays data:
public function getData ()
{
$automovel_modelo = AutomovelModelo::all();
return Datatables::of($automovel_modelo)
->addColumn('action', function($automovel_modelo){
return
'<a href="automoveis_modelos/'. $automovel_modelo->id .'/edit" class="btn btn-xs btn-warning" title="Editar">
<i class="fa fa-pencil"></i>
</a>' .
'<a onclick="deleteData('. $automovel_modelo->id .')" class="btn btn-danger btn-xs"><i class="glyphicon glyphicon-trash"></i> Delete</a>';
})
->make(true);
return datatables(AutomovelModelo::query())->toJson();
}
But I couldn’t figure out how to get the relationship to json.
It helped a lot, actually I had to pass the argument { date: 'brand.automovel_brand', name: 'brand.automovel_brand' }
– Rafael Meirim