Greetings.
I have a template intended to generate API responses from Tailor content types. My CMS is based in posts that could have several authors. So it is something like this:
title = "Posts"
layout = "api"
url = "/api-v1/posts/filter/:status?/:language?/:author?/:dates?any/:orderby?id/:order?asc/:ipp?100"
[collection]
handle = "Content\Post"
==
{% set posts = collection %}
{# Check status parameter #}
{% if this.param.status and this.param.status != 'any' %}
{% set posts = posts.where('status', this.param.status) %}
{% endif %}
{# ******* Here some other Twig checks that are working fine ********* #}
{# Check if author parameter is integer #}
{% if this.param.author matches '/^\\d+$/' %}
{# Next line doesn't work. This is my attempt to filter the posts that include the author passed in the URL parameter #}
{% set posts = posts|filter(v => v.authors and v.authors.id == this.param.author) %}
{% endif %}
{% set posts = posts.orderBy(this.param.orderby, this.param.order).paginate(this.param.ipp) %}
{% set pager = pager(posts) %}
{% do response({
data: posts,
links: pager.links,
meta: pager.meta
}) %}
I got an error:
Call to a member function toArray() on null
~/modules/system/classes/PagerElement.php line 49
I know that there’s a way to filter collections with PHP/Laravel, using the filter method and a callback function, but I can’t figure out how to implement that inside Twig.
Your comments and suggestions will be greatly appreciated.
Best regards.