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

[5.4] Use fromRawAttributes when creating a newPivot #18127

Merged
merged 1 commit into from
Feb 27, 2017
Merged

[5.4] Use fromRawAttributes when creating a newPivot #18127

merged 1 commit into from
Feb 27, 2017

Conversation

PhiloNL
Copy link
Contributor

@PhiloNL PhiloNL commented Feb 26, 2017

Casting attributes stored as JSON don't work properly when using a pivot model and setting the $casts property.

In 5.2 fromRawAttributes method was added to manually resolve this issue. #13294

When using the new "using" method, for example: $this->belongsToMany(Team::class, 'user_teams', 'user_id', 'team_id')->using(UserTeamPivot::class), the JSON is still encoded twice. It would be great if this could be changed so the fromRawAttributes method is used.

class UserTeamPivot extends Pivot
{
    protected $casts = ['permissions' => 'collection'];
}
// App\User::find(1)->teams->first()
 App\Team {#1437
     id: 1,
     name: "Team Name",
     pivot: App\UserTeamPivot {#1436
       user_id: 1,
       team_id: 1,
       permissions: ""[\"create_campaign\"]"",
     },
   }

When using the fromRawAttributes method this is resolved.

// App\User::find(1)->teams->first()
 App\Team {#1437
     id: 1,
     name: "Team Name",
     pivot: App\UserTeamPivot {#1436
       user_id: 1,
       team_id: 1,
       permissions: "["create_campaign"]",
     },
   }

@ntzm
Copy link
Contributor

ntzm commented Feb 27, 2017

I guess this also needs to be changed here?

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

Successfully merging this pull request may close these issues.

3 participants