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

ShouldQueue Job pivot relation loaded #26780

Closed
gilesrussi opened this issue Dec 7, 2018 · 1 comment
Closed

ShouldQueue Job pivot relation loaded #26780

gilesrussi opened this issue Dec 7, 2018 · 1 comment
Labels

Comments

@gilesrussi
Copy link

gilesrussi commented Dec 7, 2018

  • Laravel Version: 5.7.16
  • PHP Version:7.2.10
  • Database Driver & Version: Postgres 9.6

Description:

I have a Job class that implements "ShouldQueue" and uses the trait "serializes model".

class Test implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public function __construct(Group $group)
    {
        $this->group = $group;
    }
    public function handle() {/** do stuff **/ return;}
}

The group class has relation with user:

class Group extends Model {
  public function users() {
    return $this->belongsToMany(User::class)
        ->using(GroupUserPivot::class)
        ->withPivot('id')
        ->as('group_user_pivot');
  }
}

The user also have a relation with groups

class User extends Model {
  public function groups() {
    return $this->belongsToMany(Group::class)
        ->using(GroupUserPivot::class)
        ->withPivot('id')
        ->as('group_user_pivot');
  }
}

And the GroupUserPivot has a relation with roles (the roles the user have in that group);

class GroupUserPivot extends Pivot {
  public function roles() {
    return $this->belongsToMany(Role::class);
  }
} 

Steps To Reproduce:

On tinker:

$group = User::first()->groups()->first()
$group->group_user_pivot->load('roles')
\App\Jobs\Test::dispatch($group)

This will be the result of the serialization:

{
  "group": {
    "class": "App\\Group",
    "id": 1,
    "relations": [
      "group_user_pivot.roles",
    ],
    "connection": "pgsql"
  },
  "job": null,
  "connection": null,
  "queue": null,
  "chainConnection": null,
  "chainQueue": null,
  "delay": null,
  "chained": []
}

When de-serialized, it will try to load the relations and it will throw an error:

Illuminate\Database\Eloquent\RelationNotFoundException: 
Call to undefined relationship [group_user_pivot.roles] on model 
[App\Group in [path]/vendor/laravel/framework/src/Illuminate/Database/Eloquent
/RelationNotFoundException.php:34
@driesvints driesvints added the bug label Dec 10, 2018
@driesvints driesvints changed the title [Bug] ShouldQueue Job pivot relation loaded ShouldQueue Job pivot relation loaded Apr 8, 2019
@taylorotwell
Copy link
Member

Fixed in 2019 by #27358

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

No branches or pull requests

3 participants