Skip to content

Eloquent eager loading, bug or intended behaviour? #33266

Discussion options

You must be logged in to vote

The behavior is surprising, but I wouldn't consider it a bug. Relationships aren't meant to be built dynamically with additional queries.

Eager loading removes relationship constraints with Relation::noConstraints() and this also affects other queries that happen during this eager loading process.

Possible workarounds for your situation:

  • Always load the merchant with the user:
class User extends Authenticatable
{
    protected $with = ['merchant'];
}
  • Use eager loading for the merchant:
class Post extends Model
{
    public function comments()
    {
        return $this->hasMany(\App\Comment::class)
            ->where('priviliged', auth()->user()->load('merchant')->merchant->priviliged)…

Replies: 5 comments 7 replies

Comment options

You must be logged in to vote
1 reply
@dododedodonl
Comment options

Comment options

You must be logged in to vote
1 reply
@dododedodonl
Comment options

Comment options

You must be logged in to vote
1 reply
@dododedodonl
Comment options

Comment options

You must be logged in to vote
3 replies
@dododedodonl
Comment options

@staudenmeir
Comment options

@dododedodonl
Comment options

Comment options

You must be logged in to vote
1 reply
@dododedodonl
Comment options

Answer selected by dododedodonl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #33266 on June 18, 2020 14:12.