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

[10.x] Add WithoutRelations attribute for model serialization #47989

Merged
merged 2 commits into from
Aug 14, 2023

Conversation

Neol3108
Copy link
Contributor

@Neol3108 Neol3108 commented Aug 8, 2023

Hey all,

I normally serialize my models without their relations, I've had some issues with circular dependencies and often don't even need them after unserialization. I usually do this by calling withoutRelations on the model(s) in my constructor like this:

<?php

use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\SerializesModels;

class Queued implements ShouldQueue
{
    use SerializesModels;

    public User $user;

    public function __construct(User $user)
    {
        $this->user = $user->withoutRelations();
    }
}

There is however no way to do this with property-promotion. And apart from that I like to keep my job (etc.) constructors as slim as possible.

The following is possible with this PR:

<?php

use App\Models\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\Attributes\WithoutRelations;
use Illuminate\Queue\SerializesModels;

class Queued implements ShouldQueue
{
    use SerializesModels;

    public function __construct(
        #[WithoutRelations]
        public User $user,
    ) {
        //
    }
}

I don't know if my approach is the best but it's the first that came to mind.

I'm curious to know what you think.

@cosmastech
Copy link
Contributor

cosmastech commented Aug 8, 2023

#47097 I offered this exact same feature a few months ago and it was closed. Would love to see something like this get to merged, as it's very useful.

@Neol3108
Copy link
Contributor Author

Neol3108 commented Aug 8, 2023

@cosmastech Ah, missed that PR. That doesn't give me much hope for this version. Will leave it here for now anyway tho

@driesvints
Copy link
Member

Please mark this as ready for review if you want one. Draft PRs aren't looked at. Thanks!

@Neol3108 Neol3108 marked this pull request as ready for review August 9, 2023 06:14
@taylorotwell taylorotwell merged commit 8bea688 into laravel:10.x Aug 14, 2023
@Neol3108 Neol3108 deleted the without-relations-attribute branch August 14, 2023 19:29
@timacdonald
Copy link
Member

Love this.

@siarheipashkevich
Copy link
Contributor

Hi @Neol3108 is it possible to use this attribute for events ?

@Neol3108
Copy link
Contributor Author

Neol3108 commented Nov 8, 2023

@siarheipashkevich yea sure, anything that uses the SerializesModels trait.

@siarheipashkevich
Copy link
Contributor

@Neol3108 what do you think about adding this to the documentation?

@Neol3108
Copy link
Contributor Author

Neol3108 commented Nov 8, 2023

@siarheipashkevich I mean, it's fine by me. Feel free to PR, Taylor will have to judge I guess.

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.

6 participants