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.x] Single relationship query builders #9979

Open
wants to merge 1 commit into
base: 5.x
Choose a base branch
from

Conversation

jacksleight
Copy link
Contributor

@jacksleight jacksleight commented May 1, 2024

This PR is really intended to be an idea rather than a final implementation, the solution is a bit of a hack and a better approach is probably needed, but it would be really nice if this was possible.

At the moment if you're working in PHP and have an entry with either single or multi entries fields you can do this to fetch the published related entries:

$publishedChildren = $parent->children;
$publishedChild = $parent->child;

If you want related entries of any status you can do this with multi entries fields:

$anyChildren = $parent->children()->whereAnyStatus()->get();

But if you want a single related entry of any status there's not really a simple way to do it (I don't think). You can't do this with single entry fields, it throws a child method does not exist error:

$anyChild = $parent->child()->whereAnyStatus()->first();

When working with Eloquent models you can do $model->singleThing() with a belongs to relationship and it'll give you a query builder, it'd be great if Statamic could do the same.

This PR makes the above work by tricking any relationship fieldtypes into returning a query builder even if it's a single entry field. This only happens during __call so shouldn't affect anything else.

@jacksleight jacksleight changed the title [4.x] Single entry relationship query builders [4.x] Single relationship query builders May 1, 2024
@jacksleight jacksleight changed the base branch from 4.x to 5.x May 10, 2024 10:03
@jacksleight jacksleight changed the title [4.x] Single relationship query builders [5.x] Single relationship query builders May 10, 2024
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.

None yet

1 participant