[5.x] Single relationship query builders #9979
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
If you want related entries of any status you can do this with multi entries fields:
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: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.