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

Filter function BUILDER expected Illuminate\Database\Query\Builder but Illuminate\Database\Eloquent\Builder given #8

Open
akito85 opened this issue Apr 18, 2017 · 3 comments

Comments

@akito85
Copy link

akito85 commented Apr 18, 2017

I have this function in my User (Models)

public function drivers()
{
  return $this->belongsTo(Driver::class, 'id', 'user_id');
}
public function isDriverAvailable()
{
  return $this->drivers();
}

and then the UserRepository (Repositories)

use Illuminate\Database\Query\Builder;
class UserRepository extends Repository
{
  public function filterIsDriverAvailable(Builder $query, $method, $clauseOperator, $value, $in)
  {
      $query->whereIn('drivers.status', ['available']);
  }

It returned this error when I tried to filter using postman
"Type error: Argument 1 passed to Api\Users\Repositories\UserRepository::filterIsDriverAvailable() must be an instance of Illuminate\Database\Query\Builder, instance of Illuminate\Database\Eloquent\Builder given"

$this->belongsTo(Driver::class, 'id', 'user_id') <-- this one is Eloquent\Builder

but

filterIsDriverAvailable(Builder $query, $method, $clauseOperator, $value, $in) <-- expected arg Query\Builder

any ideas on how to resolve this?

@akito85 akito85 changed the title Builder class in filter must be from Illuminate\Database\Query\Builder? Filter function BUILDER expected Illuminate\Database\Query\Builder but Illuminate\Database\Eloquent\Builder given Apr 18, 2017
@denielaa
Copy link

@akito85 do you have solution for this?

@denielaa
Copy link

denielaa commented Jun 11, 2017

I just found out the solution for this.
you should use Eloquent\Builder instead.

but you will get another error call to undefined something
if you are using belongsTo in the relation, you will get error call to undefined builder::getotherkey()
this is because of the laravel version. as far as I know genie, bruno, and larapi are using laravel 5.2 but in laravel 5.4 some of the function name has been renamed like getOtherKey() into getOwnerKey()

so just check out bruno in EloquentBuilderTrait (last function) rename it and use the new function name.
it will solve your issue

FYI: the function name has been renamed only since 5.4 so if you are using 5.1 - 5.3 I believe that you will get no problem

@akito85
Copy link
Author

akito85 commented Jun 16, 2017

@denielaa thanks for your input, I have never known that it was written on laravel 5.2, thanks again

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