0
votes

I have the related models Request 1: M Tasks and I would like to show a datatable with yajra in the show view of the requests, that is to say that if I enter the show view of the request with id = 1 it will show me a datatable filled only with the tasks related to the request id = 1, I tried to return the datatable from the show function of the controller but I didn't know how to only get the tasks related to the request.

In any case, I don't know if this is of any use, but, this is how I make my datatable for the index of the tasks:

    public function taskData()
    {
    $tasks = Task::join('requests', 'requests.id', '=', 'tasks.request_id')
                    ->select('tasks.id', 'requests.code_request', 'tasks.fake_id',
                            'tasks.date');

    return Datatables::of($tasks)
            ->addColumn('btn', 'tasks.actions')
            ->rawColumns(['btn'])
            ->make(true);
    }

and then in my view:

<script>
   $(function() {
      $(document).ready(function(){
         // initializing Datatable
             var table = $("#tareas-table").DataTable({
                serverSide: true,
                pageLength: 10,
                ajax: '{!! route('datatables.tareas') !!}',
                columns: [
                   { data: 'fake_id', name: 'tareas.fake_id' },
                   { data: 'codigo_solicitud', name: 'solicituds.codigo_solicitud' },
                   { data: 'fecha_inicio', name: 'tareas.fecha_inicio' },
                   { data: 'estado', name: 'tareas.estado'  },
                   { data: 'btn', name: 'btn',orderable:false,serachable:false,sClass:'text-center' }
               ]
           });
        });
 </script>

thanks

1

1 Answers

0
votes

You can use the DataTableScopeContract for that.

The docs have an example for that: https://datatables.yajrabox.com/services/scope