Closed knubbe82 closed 1 year ago
See this demo https://datatables.yajrabox.com/eloquent/row-details for ref. Thanks!
That link doesnt help us add model relationship data to the table?
`$project = Project::leftJoin('users','projects.leadBy','=','users.id')->join('status', 'projects.status_id', '=', 'status.id') ->join('brand_categories', 'projects.category_id', '=', 'brand_categories.id') ->select(array('projects.id','projects.proj','projects.title','projects.launchdate','projects.leadBy','users.name as leadbyname','projects.textWritten','projects.translations','projects.images','status.name')) ->where('leadBy', \Auth::user()->id) ->where('status_id', '<', 3);
return Datatables::of($project)
->addColumn('operations','<a href="{{ URL::route( \'projects.edit\', [\'id\' => $id ]) }}" class="btn btn-sm btn-info"><i class="fa fa-edit"></i></a>')
// you can also give a function as parameter to editColumn and addColumn instead of blade string
->addColumn('send', '<a href="{{ URL::route( \'project.send\', [\'id\' => $id ]) }}" class="btn btn-sm btn-warning"><i class="fa fa-envelope"></i></a>')
->addColumn('category', ": ")
->make(true);`
As you can see I am adding a column category which is a relation of project. laravel model would be $project->category->name so how can I add this in the column?
1
I got the same issue to display a "quantity" column which is in my pivot table inside my datatable...
How can i do please?
+1 someone did it please ?
Ok so just in case if someone still looking for a solution, The main issue that when you return a hasMany
relation like the following you won't get a correct stringified JSON text that you can parse using JSON.parse
in your JS side.
So to solve this you'll need to add the relationship column to the rawColumns
array.
Here's an example.
return DataTables::eloquent(Task::with('subtasks'))
->addColumn('subtasks', function (Task $task) {
return json_encode($task->subtasks->all());
})
->rawColumns(['subtasks'])
->make(true);
function format ( data ) {
var $markup = '<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">';
var subtasks = JSON.parse(data.subtasks);
subtasks.forEach(subtask => {
$markup += '<tr><td>' + subtask.name + '</td></tr>';
})
$markup += '</table>';
return $markup;
}
$(function() {
var table = $('#tasks-table').DataTable({
processing: true,
serverSide: true,
ajax: '/admin/tasks/list',
columns: [
{
"className": 'details-control',
"orderable": false,
"data": 'expandable_icon',
"name": 'expandable_icon',
"defaultContent": ''
},
{ data: 'task_name', name: 'task_name' },
]
});
$('#tasks-table tbody').on('click', 'td.details-control', function () {
var tr = $(this).closest('tr');
var row = table.row( tr );
if ( row.child.isShown() ) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
}
else {
// Open this row
row.child( format(row.data()) ).show();
tr.addClass('shown');
}
});
});
This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.
This issue was closed because it has been inactive for 7 days since being marked as stale.
Summary of problem or feature request
I want to display orders.type and employer.name in child row (row details)
Code snippet of problem
System details