Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Fix create eloquent datatable from relation #2789

Merged
merged 2 commits into from
May 23, 2022

Conversation

sebdesign
Copy link

The DataTables::eloquent() method accepts a Illuminate\Contracts\Database\Eloquent\Builder which can be an eloquent builder, but also an eloquent relation.

Using DataTables::eloquent(User::query()) works fine, but DataTables::eloquent($user->posts()) throws the following error:

Exception Property [columns] does not exist on the Eloquent builder instance. 
    vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:1701 Illuminate\Database\Eloquent\Builder::__get
    vendor/yajra/laravel-datatables-oracle/src/QueryDataTable.php:59 Yajra\DataTables\QueryDataTable::__construct
    vendor/yajra/laravel-datatables-oracle/src/EloquentDataTable.php:39 Yajra\DataTables\EloquentDataTable::__construct
    vendor/yajra/laravel-datatables-oracle/src/DataTableAbstract.php:167 Yajra\DataTables\DataTableAbstract::create
    vendor/yajra/laravel-datatables-oracle/src/DataTables.php:126 Yajra\DataTables\DataTables::eloquent

This happens because the constructor of EloquentDataTable passes the eloquent builder to its parent, instead of the query builder.

This PR fixes the constructor of EloquentDataTable so that the correct object is passed to the QueryDataTable constructor.

@yajra
Copy link
Owner

yajra commented May 21, 2022

@sebdesign looks good but phpstan is failing. Can you please fix? WIll also try to check later. Thanks!

@sebdesign
Copy link
Author

@yajra I've removed the default arm that throws the exception, and added a type declaration to the $model argument, instead of using phpdoc. This way PHP can enforce that the $model is a valid object, instead of ensuring this using an exception.

@yajra yajra merged commit 24e71b5 into yajra:master May 23, 2022
@yajra yajra changed the title Create eloquent datatable from relation [10.x[ Fix create eloquent datatable from relation May 23, 2022
@yajra yajra changed the title [10.x[ Fix create eloquent datatable from relation [10.x] Fix create eloquent datatable from relation May 23, 2022
@yajra
Copy link
Owner

yajra commented May 23, 2022

Thanks! I was actually also planning to remove those exceptions when creating, glad you fixed it. Released on v10.0.7 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants