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

Allow morph relationships to define conditional $with attributes #261

Open
efmjackhardcastle opened this issue Oct 30, 2023 · 3 comments
Open

Comments

@efmjackhardcastle
Copy link

I have the following:

    protected array $with = [
        'eventRelationshipUpdates.fromModel.ancestors'
    ];

fromModel is morphable, some of these model types may have an ancestors relationship that needs loading, some may not, this doesn't work due to not every morphable model having this relationship defined.

For morphable relationships any $with inclusions, as well as anything present in the include query, should conditionally include if the resolving model has that relationship available and defined.

@efmjackhardcastle
Copy link
Author

I have found a workaround I will use in the meantime, on the relationship definition:

    public function fromModel(): MorphTo
    {
        return $this->morphTo()
            ->morphWith([
                MaintainableEntity::class => ['ancestorsAndSelf']
            ]);
    }

@lindyhopchris
Copy link
Contributor

Thanks for raising. I think you're going to have to stick with the workaround for now, as the eager loading needs revamping to unlock several features on the development roadmap. I'll leave this issue open though as something to look into, either as part of that revamp or once the revamp has been done.

@efmjackhardcastle
Copy link
Author

efmjackhardcastle commented Oct 30, 2023

No problem @lindyhopchris - on reflection, this workaround doesn't as much feel like one. There would be limitations if you only wanted to do this contextually under one server, however not an issue right now.

Would be nice to have the same behaviour in the future on the Schema definition.

class EventSchema extends Schema
{
    protected array $morphWith = [
        'eventRelationshipUpdates.fromModel' => [
            MaintainableEntity::class => ['ancestorsAndSelf'],
        ],
    ];
    
    ....
}

Don't have a semantic option to offer for the includes field I'm afraid!

But appreciate what you're saying in the meantime.

Thanks for the package, it's great!

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

No branches or pull requests

2 participants