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

orWherePivot issue #29741

Closed
zeyad82 opened this issue Aug 26, 2019 · 5 comments
Closed

orWherePivot issue #29741

zeyad82 opened this issue Aug 26, 2019 · 5 comments

Comments

@zeyad82
Copy link

zeyad82 commented Aug 26, 2019

  • Laravel Version: 5.8

Description:

I am trying to use orWherePivot on a BelongsToMany relationship. When I use it per user, it cancels the filtration of user_id and it's applied to the global query so it returns all the records instead of only per user, when I try to enclose it in where closure scope, it throws Call to undefined method Illuminate\Database\Eloquent\Builder::orWherePivot() which makes it also applied to the global query.

Is that a normal behavior? How can I use it only per user scope?

Steps To Reproduce:

The following returns all records not only per user
$user->BelongsToMany() ->wherePivot('pivot', 0) ->orWherePivot('another', 0)->get()

And the following throws the mentioned error
$user->BelongsToMany() ->where(function($query) { $query->wherePivot('pivot', 0) ->orWherePivot('another', 0); })->get()

@driesvints
Copy link
Member

@staudenmeir do you know of any changes to this method?

@staudenmeir
Copy link
Contributor

orWherePivot() has been around since 2013, but it looks like there has never been an actual use case. There aren't any tests either.

I'll take a look if we can make it work with closures.

You can use the "normal" where() methods as a workaround for SELECT queries:

$user->BelongsToMany()->where(function ($query) {
    $query->where('pivot_table.pivot', 0)->orWhere('pivot_table.another', 0);
})->get()

The downside here is that detach(), sync() etc. don't respect these pivot constraints (as they do with wherePivot()).

@zeyad82
Copy link
Author

zeyad82 commented Aug 27, 2019

I managed to use orWherePivot by adding the user_id manually:

$user->BelongsToMany()->wherePivot('pivot', 0)
->orWherePivot('another', 0)
->where('user_id', $user->id)->get()

@staudenmeir
Copy link
Contributor

I don't see a feasible way to make closures work.

@driesvints
Copy link
Member

Probably a no fix, sorry.

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

No branches or pull requests

3 participants