Get deleted records in API response

Hello.
I’m on the last version of October CMS.
Currently, I’m using Tailor to define content types and publish API endpoints for each entity.

For example:


title = "Authors"
layout = "api"
url = "/api-v1/authors/all/:orderby?id/:order?asc/:ipp?100"

[collection]
handle = "Content\Author"
==
{% set authors = collection.orderBy(this.param.orderby, this.param.order).paginate(this.param.ipp) %}

{% set pager = pager(authors) %}

{% set result = collect() %}

{% for author in authors %}
    {% do result.push({
        modified: author.updated_at | date('U'),
        id: author.id,
        name: author.title,
        description: author.description,
        photo: author.photo.path,
    }) %}    
{% endfor %}

{% do response({
    data: result,
    links: pager.links,
    meta: pager.meta
}) %}

How can I modify this to include the deleted records, meaning the records with some value in the “deleted_at” field?

Also, I would appreciate some advice regarding a simple way to implement recovery of soft-deleted records, since by default there is no trash bin.

Thanks in advance.

You can use a where clause to select those as well and push it to the array

{% set deleted_authors = collection.where('deleted_at').orderBy(this.param.orderby, this.param.order).paginate(this.param.ipp) %}

Thanks for your suggestion. However, it doesn’t work. I get the same results as if not using the ‘where’ clause at all.

I also tried:

{% set authors = collection.whereNotNull('deleted_at').orderBy(this.param.orderby, this.param.order).paginate(this.param.ipp) %}

Or:

{% set authors = collection.where('deleted_at', '!=', NULL).orderBy(this.param.orderby, this.param.order).paginate(this.param.ipp) %}

In both cases I get no records in the results.

are you referencing authors twice? If so, that would definitely be the issue. It would pull from the active records, which would return nothing

{% set authors = collection.orderBy(this.param.orderby, this.param.order).paginate(this.param.ipp) %}

{% set authors = collection.whereNotNull('deleted_at').orderBy(this.param.orderby, this.param.order).paginate(this.param.ipp) %} {# would return null since you already got the ones that are not deleted #}

Try this for me and see if it works

{% set authors = collection.orderBy(this.param.orderby, this.param.order).paginate(this.param.ipp) %}

{% set deleted_authors = collection.whereNotNull('deleted_at').orderBy(this.param.orderby, this.param.order).paginate(this.param.ipp) %}

{{ dd(authors) }}
{{ dd(deleted_authors) }}

Nope. I used those code variants separately.

The response for deleted authors is empty, despite that I have soft deleted records.

Regards.

The only thing that I could think of why it’s not returning is that you may need to put .get() at the end since it’s using a where query. Other than that I’m stumped

Use the withTrashed method

So like

{% set deleted_authors = collection
    .withTrashed()
    .orderBy(this.param.orderby, this.param.order)
    .paginate(this.param.ipp) %}
1 Like

Your solution does the trick.

Thanks.