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.5] Apply custom pivot model attribute casting on arrays #21275

Merged
merged 2 commits into from
Sep 20, 2017
Merged

[5.5] Apply custom pivot model attribute casting on arrays #21275

merged 2 commits into from
Sep 20, 2017

Conversation

themsaid
Copy link
Member

Currently attaching/syncing multiple records doesn't trigger the attribute casting on the custom pivot model:

$project->collaborators()->attach([
            [$user->id => ['permissions' => ['foo' => 'bar']]],
            [$user2->id => ['permissions' => ['baz' => 'bar']]],
        ]);

class CustomPivotModel extends Pivot
{
    protected $casts = [
        'permissions' => 'json',
    ];
}

@chelout
Copy link

chelout commented Sep 20, 2017

Try this case, it fails:

    public function test_casts_are_respected_on_sync_array()
    {
        $user = CustomPivotCastTestUser::forceCreate([
            'email' => '[email protected]',
        ]);

        $user2 = CustomPivotCastTestUser::forceCreate([
            'email' => '[email protected]',
        ]);

        $project = CustomPivotCastTestProject::forceCreate([
            'name' => 'Test Project',
        ]);

        $project->collaborators()->attach([
            $user->id => ['permissions' => ['foo' => 'bar']],
            $user2->id => ['permissions' => ['baz' => 'bar']],
        ]);
        $project->collaborators()->sync([
            $user->id => ['permissions' => ['foo1' => 'bar1']],
            $user2->id => ['permissions' => ['baz2' => 'bar2']],
        ]);
        $project = $project->fresh();

        $this->assertEquals(['foo' => 'bar'], $project->collaborators[0]->pivot->permissions);
        $this->assertEquals(['baz' => 'bar'], $project->collaborators[1]->pivot->permissions);
    }

@themsaid
Copy link
Member Author

@chelout if course it does, sync re-creates the records, your assertions are invalid.

@chelout
Copy link

chelout commented Sep 20, 2017

Sorry, was in a hurry. Test throws error:

Illuminate\Tests\Integration\Database\EloquentCustomPivotCastTest::test_casts_are_respected_on_sync_array
ErrorException: Array to string conversion

Right assertions:

    public function test_casts_are_respected_on_sync_array()
    {
        $user = CustomPivotCastTestUser::forceCreate([
            'email' => '[email protected]',
        ]);

        $user2 = CustomPivotCastTestUser::forceCreate([
            'email' => '[email protected]',
        ]);

        $project = CustomPivotCastTestProject::forceCreate([
            'name' => 'Test Project',
        ]);

        $project->collaborators()->attach([
            $user->id => ['permissions' => ['foo' => 'bar']],
            $user2->id => ['permissions' => ['baz' => 'bar']],
        ]);
        $project->collaborators()->sync([
            $user->id => ['permissions' => ['foo1' => 'bar1']],
            $user2->id => ['permissions' => ['baz2' => 'bar2']],
        ]);
        $project = $project->fresh();

        $this->assertEquals(['foo1' => 'bar1'], $project->collaborators[0]->pivot->permissions);
        $this->assertEquals(['baz2' => 'bar2'], $project->collaborators[1]->pivot->permissions);
    }

@themsaid
Copy link
Member Author

@chelout ah thanks, check now.

@chelout
Copy link

chelout commented Sep 20, 2017

Seems fine. Thanks a lot!

@taylorotwell taylorotwell merged commit cdc3f42 into laravel:5.5 Sep 20, 2017
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