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

Exceptions using Query Builder of Laravel #3

Closed
ozza opened this issue Jun 23, 2016 · 9 comments
Closed

Exceptions using Query Builder of Laravel #3

ozza opened this issue Jun 23, 2016 · 9 comments

Comments

@ozza
Copy link

ozza commented Jun 23, 2016

Hello,

First, thanks for the all of your work.

I am trying to use the Grid component in my Laravel project with eloquent-data-processing library.

It is working well while using eloquent data models, but not working with using the query builder as data source.

I'm using it like:

use DB;
$query = DB::select('select * from table_name where ID = 1');
or
$query = DB::table('table_name')->where('id','=','1');
with
$provider = new EloquentDataProvider($query);

For the first one I'm getting:

InvalidDataSourceException in InvalidDataSourceException.php line 18:
Invalid data source, EloquentDataProvider constructor should be used with Illuminate\Database\Eloquent\Builder instance or Illuminate\Database\Query\Builder instance or class name of target Eloquent model(array given).

And for the second one:

ErrorException in Builder.php line 2345:
Call to undefined method Illuminate\Database\Query\Builder::getQuery()

Obviously I'm doing something wrong. I need to get it work by using raw sql queries. Can you help me with the issue?

Thank you.

@Nayjest
Copy link
Member

Nayjest commented Jun 23, 2016

Hi!
What is your version of Laravel?

@ozza
Copy link
Author

ozza commented Jun 23, 2016

Hello again,

My Laravel version is: 5.2.39 (latest I think)

@Nayjest
Copy link
Member

Nayjest commented Jun 23, 2016

First of all, EloquentDataProvider it will not work with DB::select() becouse it returns array of results.
You can use DB::select with PhpArrayDataProvider, but in this case grids will sort / filter phph arrays instead of modifying SQL query ant that's not a best solution regarding performance.

Second example should work, I will try to chek it now.

@Nayjest
Copy link
Member

Nayjest commented Jun 23, 2016

Can you provide exception trace for second exception:

ErrorException in Builder.php line 2345:
Call to undefined method Illuminate\Database\Query\Builder::getQuery()

@Nayjest
Copy link
Member

Nayjest commented Jun 23, 2016

Looks like I found a bug.
I will try to remove ->getQuery() in \ViewComponents\Eloquent\Processor\PaginateProcessor, becouse it works only for Illuminate\Database\Eloquent\Builder and in your case there is Illuminate\Database\Query\Builder

You can try to remove this method call from \ViewComponents\Eloquent\Processor\PaginateProcessor in your vendors folder (temporary) and give me know if it helped. If so, I can release bugfix just now

@ozza
Copy link
Author

ozza commented Jun 23, 2016

Sorry I couldn't catch you up.

I commented the

//$src->getQuery()
//            ->limit($operation->getPageSize())
//            ->offset($this->getOffset($operation));

section and returned the $src untouched, and it seems working now.

@Nayjest
Copy link
Member

Nayjest commented Jun 23, 2016

Bugfix released in v1.2.1
Please update view-components/eloquent-data-processing to version 1.2.1

@Nayjest
Copy link
Member

Nayjest commented Jun 23, 2016

And use it this way:

$query = DB::table('table_name')->where('id','>','1');
$provider = new EloquentDataProvider($query);

Or

$provider = new EloquentDataProvider(MyEloquentModel::class);

Or

$provider = new EloquentDataProvider(MyEloquentModel::where('id','>','1'));

@ozza
Copy link
Author

ozza commented Jun 23, 2016

Thank you very much.

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

No branches or pull requests

2 participants