Framework: [BUG][5.6] undefined relationship [pivot] on model [App\Models\User] when broadcasting - sync queue

Created on 8 Feb 2018  路  11Comments  路  Source: laravel/framework

  • Laravel Version: 5.6.0
  • PHP Version: 7.2.2
  • Database Driver & Version: Mysql 5.7.2

Description:

I just updated my Laravel version to 5.6 and updated the files from github.
Then running my tests I started getting the mentioned error on all the tests that broadcast. So I'm holding up on updating my production code.

Stack trace:

Illuminate\Database\Eloquent\RelationNotFoundException : Call to undefined relationship [pivot] on model [App\Models\User].
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Database\Eloquent\RelationNotFoundException.php:34
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php:546
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Relations\Relation.php:89
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php:548
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php:516
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php:496
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:392
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Queue\SerializesAndRestoresModelIdentifiers.php:85
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Queue\SerializesAndRestoresModelIdentifiers.php:55
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Queue\SerializesModels.php:45
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Queue\CallQueuedHandler.php:146
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Queue\Jobs\Job.php:172
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Queue\FailingJob.php:33
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Queue\SyncQueue.php:119
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Queue\SyncQueue.php:46
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Queue\Queue.php:44
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Broadcasting\BroadcastManager.php:125
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php:271
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php:194
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Notifications\Channels\BroadcastChannel.php:51
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Notifications\NotificationSender.php:113
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Notifications\NotificationSender.php:89
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Notifications\NotificationSender.php:64
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Notifications\ChannelManager.php:35
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Notifications\RoutesNotifications.php:18
C:\wamp64\www\barrioBall\app\Listeners\User\GetMessage.php:30
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php:369
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php:200
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php:465
C:\wamp64\www\barrioBall\app\Http\Requests\User\SendMessageRequest.php:34
C:\wamp64\www\barrioBall\app\Http\Controllers\User\ConversationController.php:59
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Controller.php:54
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php:45
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Route.php:212
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Route.php:169
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Router.php:659
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:30
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authorize.php:57
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:149
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
C:\wamp64\www\barrioBall\app\Http\Middleware\Language.php:28
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:149
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Middleware\SubstituteBindings.php:41
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:149
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php:43
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:149
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php:67
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:149
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php:49
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:149
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php:63
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:149
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse.php:37
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:149
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php:59
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:149
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:102
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Router.php:661
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Router.php:636
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Router.php:602
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Router.php:591
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:176
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:30
C:\wamp64\www\barrioBall\vendor\fideloper\proxy\src\TrustProxies.php:57
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:149
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php:30
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:149
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php:30
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:149
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php:27
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:149
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php:46
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:149
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:102
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:151
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:116
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:345
C:\wamp64\www\barrioBall\vendor\laravel\framework\src\Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.php:195
C:\wamp64\www\barrioBall\tests\Feature\Conversation\ConversationTest.php:112

Steps To Reproduce:

  1. Clone laravel5.6 https://github.com/el-coop/barrioBall
  2. Composer install
  3. Run phpunit tests
needs more info

Most helpful comment

I am also experiencing the same issue when upgrading from 5.5 to 5.6
This is not fixed.

As a workaround I'm calling ->fresh() before passing it to the constructor of the job.

All 11 comments

Yes same issue here, I have removed SerializesModels since i am not queing only broadcasting and everything is back to normal.

We can't find the bug looking at your entire code base, please include simple steps to replicate the issue.

Same issue here...

This a dd(...) from the Illuminate\Queue\SerializesAndRestoresModelIdentifiers@restoreModel trait艣 method:

~php
Illuminate\Contracts\Database\ModelIdentifier {#951
+class: "App\Models\User"
+id: 2
+relations: array:3 [
0 => "tenant"
1 => "roles"
2 => "roles.pivot"
]
+connection: "system"
}
~

It tries to load the nested pivot relation from the role relation, although this is an automatic relation set by Laravel when using a BelongsToMany relationship and it is not explicitly defined in theRole model

I don't know if it is better to filter the pivot related relation from the `ModelIdentifier' object when serializing, or to check if a relation is explicitly defined in the model when restoring from that object...

I'll try to figure out and work out a fix to this, but in the meantime I'm holding on upgrading my production app to 5.6

Steps to reproduce:

  1. create a new Laravel 5.6 app
  2. configure queue to use a driver that demands model serialization (e.g. Redis)
  3. Load an User instance with a BelongsToMany relation loaded (e.g. $user = User::with('roles')->first())
  4. Send a notification to that user
  5. run php artisan queue:work
  6. check the logs

I submitted PR #23081 to try to fix this issue

Hi,

This is my first message on Stackoverflow, so please let me know if I miss stating any required information.

I just updated my Laravel app to version 5.6(.21) from 5.5.* and still have the same problem that the pivot relations are loaded when serializing the models for a job. So all my notification jobs break as the notifiable is a user with many settings containing a pivot relation to check what notifications to send (email, database, etc.). Seeing this card I believe this should've been fixed already?

Best, Jorrit.

I still have the same issue even if the queue driver is sync. I'm on Laravel version 5.6.23. This breaks all my notifications :/

Are you guys sure that this bug doesn't exist anymore in the latest Laravel version? I still have this problem and already opened a discussion at Laracasts, but nobody could find a solution: https://laracasts.com/discuss/channels/laravel/call-to-undefined-relationship-pivot-on-model-appmodelsuser-when-using-queues

I am also seeing this issue on an application I am attempting to uplift from 5.5 to 5.6.

I am also experiencing the same issue when upgrading from 5.5 to 5.6
This is not fixed.

As a workaround I'm calling ->fresh() before passing it to the constructor of the job.

I've opened a PR where I've identified the problem. This should resolve the issues that we're having here.

Was this page helpful?
0 / 5 - 0 ratings