Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jrdavidson's avatar

Yajra Datatables Query method being compatible error

If anyone has used the Yajra Datatables package, I am getting this error.

ReflectionException: Class App\DataTables\UsersDataTable does not exist

Caused by
ErrorException: Declaration of App\DataTables\UsersDataTable::query(): Illuminate\Database\Query\Builder should be compatible with Yajra\DataTables\DataTables::query($builder)

From what I see both query methods are returning the same object.

<?php

namespace App\DataTables;

use App\Models\User;
use App\Filters\UserFilters;
use Yajra\DataTables\DataTables;

class UsersDataTable extends DataTables
{
    /** @var userFilters */
    private $userFilters;

    /**
     * UsersDataTable constructor.
     *
     * @param UserFilters $userFilters
     */
    public function __construct(UserFilters $userFilters)
    {
        $this->userFilters = $userFilters;
    }

    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query): \Yajra\DataTables\DataTableAbstract
    {
        return datatables($query)
            ->editColumn('started_at', function (User $user) {
                return $user->currentEmployment->started_at->format('Y-m-d H:s');
            })
            ->filterColumn('id', function ($query, $keyword) {
                $query->where($query->qualifyColumn('id'), $keyword);
            })
            ->filterColumn('name', function ($query, $keyword) {
                $sql = "CONCAT(user.first_name, ' ', user.last_name)  like ?";
                $query->whereRaw($sql, ["%{$keyword}%"]);
            })
            ->addColumn('action', 'users.partials.action-cell');
    }

    /**
     * Get query source of dataTable.
     *
     * @return \Illuminate\Database\Query\Builder
     */
    public function query(): \Illuminate\Database\Query\Builder
    {
        $query = User::with('employment');

        $this->userFilters->apply($query);

        return $query;
    }
}
0 likes
19 replies
Nakov's avatar
Nakov
Best Answer
Level 73

@xtremer360 maybe try this import:

use Yajra\DataTables\Services\DataTable;

instead of :

use Yajra\DataTables\DataTables;

and then:

class UsersDataTable extends DataTable
jrdavidson's avatar

@nakov

I don't have that class installed in the package files. This is the package that is installed.

"yajra/laravel-datatables-oracle": "~9.0"
jrdavidson's avatar

@nakov I appreciate your help on this however I'm not understanding where the Services directory is coming from since one does not exist.

Nakov's avatar

@xtremer360 sorry, that's from an old version. I guess your problem at the moment is you are type hinting the return value from the methods:

public function query(): \Illuminate\Database\Query\Builder

remove the return type, because it is probably not compatible with the library, use just this:

public function query()
jrdavidson's avatar

I've removed it and still get the same error as not being compatible with its parent method. I've added a dd() at the top of the query method and at no point does my tests ever hit that method so I don't understand how it could give that error.

jrdavidson's avatar

@nakov This is the controller code.

public function index(Request $request, UsersDataTable $dataTable, UsersFilters $requestFilter)
    {
        $this->authorize('viewList', Manager::class);

        if ($request->ajax()) {
            $query = User::query();
            $requestFilter->apply($query);

            return $dataTable->eloquent($query)->toJson();
        }

        return view('users.index');
    }
Nakov's avatar

@xtremer360 I don't understand why you get this

ReflectionException: Class App\DataTables\UsersDataTable does not exist

at the first place. Does the file exists under that namespace? Make sure that the directory is called DataTables and the file is UsersDataTable.php

jrdavidson's avatar

Yes, it is. So should I be calling the eloquent method in my controller?

Nakov's avatar

@xtremer360 when you removed the return type, did the error changed, or you still get the same:

Caused by ErrorException: Declaration of App\DataTables\UsersDataTable::query(): Illuminate\Database\Query\Builder should be compatible with Yajra\DataTables\DataTables::query($builder)

?

jrdavidson's avatar

Still the same. I think it's because I don't have the eloquent method in the UsersDataTable class and its calling the parent class method.

Nakov's avatar

@xtremer360 and does just using this:

return datatables()->of(User::query())->toJson();

works ? Without applying the filters, but just curious. I mean I am using it this way, did not extend the DataTable before, plus the documentation site of Yajra is down for few days now, so cannot see the proper usage.

jrdavidson's avatar

@nakov Yeah I'm trying to figure out when I need to call the datatables and query methods on the child Datatables class from the controller.

jrdavidson's avatar

@nakov I ended up trying the above and it works, but I need to make sure that I can customize that table so that's why I was extending it.

Nakov's avatar

@xtremer360 I understand. I just wanted to see if it will work or still throw similar error. If he gets his site up soon, we can check the exact documentation, or you could try opening an issue on the GitHub page, if there isn't already one for your case.

jrdavidson's avatar

I just wonder if there's anyone else who has used this package before and would be able to give additional feedback for my post.

jrdavidson's avatar

@nakov I had to install yajra/laravel-datatables-buttons which I wasn't aware of as well.

Please or to participate in this conversation.