Skip to content

Entries fieldtype augments to a query builder - #5238

Merged
jasonvarga merged 9 commits into
masterfrom
feature/entries-augment-query-builder
Feb 9, 2022
Merged

jasonvarga merged 9 commits into
masterfrom
feature/entries-augment-query-builder

Conversation

@jasonvarga

Copy link
Copy Markdown
Member

Extracted from #5201

The entries fieldtype previously augmented to an EntryCollection of Entry objects.

Now, it'll augment to a query builder so you can continue to chain onto it and further refine your query.

Blade

This PR helps allow better fluent templating in Blade. See #5201.

@foreach ($entry->related_entries()->limit(3)->orderBy('title')->where('featured', true)->get() as $related)
  {{ $related->title }}
@endforeach

Antlers

This PR makes Antlers handle query builders in more places. You can call the same methods that you would use on a collection tag.

{{ related_posts limit="3" sort="title" featured:is="true" }}
  <li>{{ title }}</li>
{{ /related_posts }}

Unlike Blade or raw PHP, you don't have to call ->get() on the query. Antlers will know you intend to loop over the results of the query.

Breaking change

This is a bit of a breaking change when using entries fieldtypes in Antlers.

Previously, if you did:

{{ related_posts foo="bar" }}
  ...
{{ /related_posts }}

...it would be applying the foo modifier directly to the collection. But now it'll assume you're trying to modify the query.

If you want to continue to just apply modifiers to the collection, you can use an alias and put your modifiers on the inner loop.

{{ related_posts as="whatever" }}
  {{ whatever foo="bar" }}
    ...
  {{ /whatever }}
{{ /related_posts }}

@jasonvarga
jasonvarga marked this pull request as ready for review February 8, 2022 22:36
@jasonvarga
jasonvarga merged commit c484ecd into master Feb 9, 2022
@jasonvarga
jasonvarga deleted the feature/entries-augment-query-builder branch February 9, 2022 17:26
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.

2 participants