noblemfd's avatar

view blade search filter and pagination disappears

In my Laravel-5.8 application, when i used DB raw and Query Builder, the pagination and search filters in the view blade datatable disappears.

Controller

    $datas = DB::table('hr_employees AS e')
                ->leftJoin('appraisal_goals AS a', function($join) use ($identities)
                    {
                        $join->on('a.employee_id', '=', 'e.id')
                        ->where('a.appraisal_identity_id', '=', $identities)
                        ->whereNull('deleted_at');
                    })
                ->join('hr_departments AS d', function($join) use ($userCompany)
                    {
                        $join->on('e.department_id', '=', 'd.id')
                        ->where('d.company_id', '=', $userCompany);;
                    })                        
                ->join('hr_work_locations AS l', function($join) use ($userCompany)
                    {
                        $join->on('l.id', '=', 'e.work_location_id')
                        ->where('l.company_id', '=', $userCompany);;
                    })  
                ->leftJoin('hr_employees AS em', function($join) use ($userCompany)
                    {
                        $join->on('em.employee_code', '=', 'e.line_manager_id')
                        ->where('em.company_id', '=', $userCompany)
                        ->where('em.hr_status', '=', '0')
                        ->where('em.validation_status', '=', 'VALID')
                        ->whereNotIn('em.employee_type_code', [4, 5]);
                    }) 
                     ->where('e.company_id', '=', $userCompany)
                ->where('e.hr_status', '=', '0')
                ->where('e.validation_status', '=', 'VALID')
                ->whereNotIn('e.employee_type_code', [4, 5])
                ->select(
                        'e.employee_code',
                        DB::raw('CONCAT(e.first_name, " ", e.last_name) AS fullname'),
                        'e.email',
                        'd.dept_name',
                        'l.location_name',
                        'e.grade_level_name',
                        DB::raw('CONCAT(em.first_name, " ", em.last_name) AS manager'),
                        'em.email AS manager_email'                           )
                ->distinct()
                ->get(); 

    return view('employee_goal_reports')
                  ->with('datas', $datas);                       

view

            <tbody>
                @foreach($datas as $key => $data)
                    <tr data-entry-id="{{ $data->id }}">
                        <td>

                        </td>
                        <td>
                            {{ $data->employee_code ?? '' }}
                        </td>
                        <td>
                            {{ $data->fullname ?? '' }}
                        </td>

                    </tr>
                @endforeach
            </tbody> 


<script>
    $(function () {
  let deleteButtonTrans = '{{ trans('global.datatables.delete') }}'
  let deleteButton = {
    text: deleteButtonTrans,
    url: "{{ route('admin.products.massDestroy') }}",
    className: 'btn-danger',
    action: function (e, dt, node, config) {
      var ids = $.map(dt.rows({ selected: true }).nodes(), function (entry) {
          return $(entry).data('entry-id')
      });

      if (ids.length === 0) {
        alert('{{ trans('global.datatables.zero_selected') }}')

        return
      }

      if (confirm('{{ trans('global.areYouSure') }}')) {
        $.ajax({
          headers: {'x-csrf-token': _token},
          method: 'POST',
          url: config.url,
          data: { ids: ids, _method: 'DELETE' }})
          .done(function () { location.reload() })
      }
    }
  }
  let dtButtons = $.extend(true, [], $.fn.dataTable.defaults.buttons)
@can('product_delete')
  dtButtons.push(deleteButton)
@endcan

  $('.datatable:not(.ajaxTable)').DataTable({ buttons: dtButtons })
})

</script>

I just discovered that the general search filter and pagination are not showing

But when I have something like this, it appears:

public function index()
{
        $worklocations = HrWorkLocation::where('company_id', $userCompany)->get();
    
    return view('hr.work_locations.index')->with('worklocations', $worklocations);
}

https://i.stack.imgur.com/BD8SR.png

How do I resolve this?

Thanks

0 likes
1 reply
noblemfd's avatar
noblemfd
OP
Best Answer
Level 9

Resolved. I added this:

<script>
$(document).ready(function() {
    $('#myTable').DataTable({
        stateSave: true,
    });
});
</script>


<table id="myTable">

Please or to participate in this conversation.