Skip to content

Can't filter data when using redis cache #3208

Closed
@razorext2

Description

@razorext2

Summary of problem or feature request

Hello, @yajra ,
I'm sorry to bother you, but this issue is very confusing.

I'm implementing caching using Redis to cache my query. When I display all the data, it works perfectly.

However, the error occurs when I apply any of the manual filters I've created. It returns the following exception:
Exception Message: Method Yajra\DataTables\CollectionDataTable::where does not exist.

This is very strange because when I don't use caching, the query works perfectly.

Please helpppp

Code snippet of problem

here is the preview for some of my codes.
Controller.php

public function index(Request $request)
	{
		if ($request->ajax()) {
			$query = Cache::remember('dayoff_all_datas', 1800, function () {
				$data = Dayoff::with('pegawaiRelasi:kode_pegawai,full_name');

				if (!Auth::user()->can('dayoff-confirm')) {
					$data->where('kode_pegawai', Auth::user()->kode_pegawai);
				}

				return $data->get(); // Do not call ->get() here
			});

			// Pass the query to DataTables::eloquent
			return DataTables::of($query)
				->addIndexColumn()
				// Some columns using rendered blade templating
				->filter(function ($query) use ($request) {
					if ($request->filled("dayoff_for")) {
						$query->where('dayoff_for', "=", $request->dayoff_for);
					}

					if ($request->filled("kode_pegawai")) {
						$query->where('kode_pegawai', "LIKE", "%{$request->kode_pegawai}%");
					}

					if ($request->filled("status")) {
						$query->where('status', "=", $request->status);
					}

					if ($request->filled("startDate") && $request->filled("endDate")) {
						$query->whereBetween('created_at', [$request->startDate, $request->endDate]);
					}
				})
				// ->orderColumn('created_at', '-created_at $1')
				->rawColumns(['status', 'kode_pegawai', 'created_at', 'tgl_dari', 'actions'])
				->toJson();
		}

		return view('dashboard.dayoff.index');
	}

js/collect/showData.js

export function showDatatables() {
  // Initialize DataTable
  let table = $('#table-dayoff').DataTable({
    processing: true,
    serverSide: true,
    responsive: true,
    "lengthMenu": [15, 25, 50, 75, 100, -1],
    ajax: {
      url: dayoffIndex,
      data: function (d) {
        d.dayoff_for = $('#dayoff-for').val();
        d.kode_pegawai = $('#kode-pegawai').val();
        d.status = $('#status').val();
        d.startDate = $('#datepicker-range-start').val();
        d.endDate = $('#datepicker-range-end').val();
      }
    },
    columns: [
    // some columns
],
    // some configuration
  });

  $('#cari').click(function () {
    // get value from input filters
    const filters = ['#dayoff-for', '#kode-pegawai', '#status', '#datepicker-range-start',
      '#datepicker-range-end'
    ].map(selector => $(
      selector).val());

    // cek if the value is null
    if (filters.some(value => value !== '')) {
      table.draw();
    }
  });

  // if clear button clicked
  $('#clear').click(function () {
    // get all value from input filter
    const filters = ['#dayoff-for', '#kode-pegawai', '#status', '#datepicker-range-start',
      '#datepicker-range-end'
    ].map(selector => $(
      selector).val());
    // check if filter is null
    if (filters.some(value => value !== '')) {

      // nullify all the values
      $('#dayoff-for').val('');
      $('#kode-pegawai').val('');
      $('#status').prop('selectedIndex', 0);
      $('#datepicker-range-start').val('');
      $('#datepicker-range-end').val('');

      table.draw();
    }
  });
}

here's some pics:
datatables can show all of the data
image

but if i select to filter
image

it will give me the error like this:
image

System details

  • Operating System: Windows 11 Pro 23H2
  • PHP Version: PHP 8.2.22 NTS
  • Laravel Version: Laravel 11.36
  • Laravel-Datatables Version: 11

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions