Skip to content

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

Merged
taylorotwell merged 2 commits into
laravel:10.xfrom
Neol3108:without-relations-attribute
Aug 14, 2023
Merged

[10.x] Add WithoutRelations attribute for model serialization#47989
taylorotwell merged 2 commits into
laravel:10.xfrom
Neol3108:without-relations-attribute

Conversation

@Neol3108

@Neol3108 Neol3108 commented Aug 8, 2023

Copy link
Copy Markdown
Contributor

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

cosmastech commented Aug 8, 2023

Copy link
Copy Markdown
Contributor

#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

Neol3108 commented Aug 8, 2023

Copy link
Copy Markdown
Contributor Author

@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
Copy Markdown
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
Copy Markdown
Member

Love this.

@siarheipashkevich

Copy link
Copy Markdown
Contributor

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

@Neol3108

Neol3108 commented Nov 8, 2023

Copy link
Copy Markdown
Contributor Author

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

@siarheipashkevich

Copy link
Copy Markdown
Contributor

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

@Neol3108

Neol3108 commented Nov 8, 2023

Copy link
Copy Markdown
Contributor Author

@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