FerdinandFrank's avatar

Call to undefined relationship [pivot] on model [App\Models\User] when using queues

On some events and notifications that implement the ShouldQueueinterface, I receive the following error when triggering the actions via the browser (everything works via console when executing tests):

Call to undefined relationship [pivot] on model [App\Models\User].

As soon as I remove the interface declaration, everything works. But, I would really like to queue the notifications and events.

The error is caused by this relation on a model App\Models\Organizer:

 public function members() {
        return $this->belongsToMany(User::class,'organizer_members', 'organizer_id', 'user_id')
                    ->using(OrganizerMember::class)
                    ->withPivot('role_id', 'accepted')
                    ->withTimestamps();
 }

When I modify the relation by adding a ->as('membership'), I receive the following error:

Call to undefined relationship [membership] on model [App\Models\User].

So, this should be the error causing relation.

I guess the error occurs on every event or notification that implements the ShouldQueue interface and manages a variable with a relation to the App\Models\Organizer model.

Is this a Laravel bug or am I doing something wrong? I'm using Laravel v5.6.23.

0 likes
9 replies
FerdinandFrank's avatar

Thanks for your help! But yes, the class does both. It also works fine when running the tests in the console. But as soon as I trigger the notifications live on the browser it fails with the exception.

I think the bug already existed in an older Laravel version but should have got fixed, see here https://github.com/laravel/framework/issues/23068

Of course, I also got the changes for the fix in my vendor code, as I'm using the latest Laravel version, but the bug still exists on my app.

FerdinandFrank's avatar

Yes, so I have this relationship on the App\Models\Organizermodel:

 public function members() {
        return $this->belongsToMany(User::class,'organizer_members', 'organizer_id', 'user_id')
                    ->using(OrganizerMember::class)->as('membership')
                    ->withPivot('role_id', 'accepted')
                    ->withTimestamps();
 }

Then I have this relationship on the App\Models\User model:

public function organizers() {
        return $this->belongsToMany(Organizer::class, 'organizer_members', 'user_id', 'organizer_id')
                    ->using(OrganizerMember::class)
                    ->as('member')
                    ->withPivot('role_id', 'accepted', 'requested');
    }

And on the custom model to represent the intermediate table I have those two:

public function organizer() {
        return $this->belongsTo(Organizer::class);
    }

public function user() {
        return $this->belongsTo(User::class);
    }

But on the error message it says that it's missing a relationship membership on the App\Models\User model. But how shall I define this relationship :/

bobbybouwmann's avatar

Have you tried swapping membership with pivot? Like the original key that is used there and see if that works for you?

FerdinandFrank's avatar

Yes, it outputs the same error, but with pivotinstead of membership:

Call to undefined relationship [pivot] on model [App\Models\User].
bobbybouwmann's avatar

Mmh that is weird! I would recommend to create an issue on the laravel/framework repository and also provide an example in code!

I have no idea what it is without the full code in front of me!

FerdinandFrank's avatar
FerdinandFrank
OP
Best Answer
Level 9

Yes you are right. So, I couldn't reproduce the error on a fresh installation of Laravel. I guess my project is already just too big with too complex relationship definitions that causes the error. So, one event the error occurred is when I try to attach a member to an organizer and send a notification on a queue with the organizer as a parameter:

$organizer->members()->attach($user);
$user->notify(new OrganizerMemberInvitedNotification($organizer));

I noticed that when I load a fresh copy of the organizer from the database, everything works fine:

$organizer->members()->attach($user);
$user->notify(new OrganizerMemberInvitedNotification($organizer->fresh()));

So, I guess the problem is related to the loaded members relationship on the Organizer model, but I don't know if this is an issue with the Laravel framework or an error in my code. But, I'm fine with just loading a fresh copy of the organizer from the database to solve this issue! :)

warren32's avatar

This worked for me @ThisIsEpic . Thanks for the suggestion. I had an issue where the entities were not getting unserialized properly and ->fresh() did the trick.

Please or to participate in this conversation.