eloquent-driver
eloquent-driver copied to clipboard
Content API: Selecting Fields not working / error
Bug Description
If you try to select just specific fields (exactly as described in the documentation), nothing happens or an error will be thrown.
How to Reproduce
Problem 1: Selecting Fields has no effect
Request
http://statamic.test/api/collections/newsletters/entries?fields=title
Response
I would expect only the `title` attribute of each entry shown here.
Problem 2: Error when specifying specific fields
Request
http://statamic.test/api/collections/newsletters/entries?fields=id
or
http://statamic.test/api/collections/newsletters/entries?fields=date
or
http://statamic.test/api/collections/newsletters/entries?fields=uri
Response
Error: Call to a member function cascade() on null

Environment
Statamic 3.1.32 Pro Laravel 8.40.0 PHP 7.4.19 statamic/collaboration 0.1.0
Install method (choose one):
- Fresh install from
statamic/statamic
This is working fine for me. Perhaps it's something unique about your setup. Would you be able to zip up your site and send it to [email protected] for us to take a quick look?
The issue here is the eloquent driver. I'll move the issue over to that repo.
So this works fine for me on v1, as long as one of the fields is collection... if you dont select it then you dont get the blueprint, so it throws an error when trying to augment fields.
So either we update the code to always select collection, or we just make people aware they need to?
This is something we're experiencing as well. Including the "collection" field seems to be a workaround for now, but a real fix would be great!
I think the solution will be to always select collection even when it's not asked for. But yep leave it with me and I'll try and come up with something before the next release.
I've opened a pull request here which should solve the issue, though I need to spend some time sitting on it to decide if (a) there isnt a better way, (b) this doesnt affect other areas of the driver
It turns out adding the collection field doesn't actually resolve this. The query doesn't fail, but all the fields of the entry are null when the collection field is included.
@Smef are you able to supply an example repository showing the issue?
@Smef did you try applying the changes made here: https://github.com/statamic/eloquent-driver/pull/183
I tried updating the EntryQueryBuilder.php file with the changes in that PR. It didn't seem to make a difference with the returned data.
Here's an example
fields=title,price_base
returns:
{
"data": [
{
"title": "magnam velit accusamus et eius",
"price_base": 37.25
}
],
...
adding id and collection:
fields=id,collection,title,price_base
{
"data": [
{
"id": 332,
"collection": {
"title": "Products",
"handle": "products"
},
"title": null,
"price_base": null
}
...
@Smef works fine for me with the PR - double check you copied everything over fully:
visiting: /api/collections/pages/entries?fields=id,title,template,shares,content
{
"data": [
{
"id": "4a8b8690-55c6-426d-8d08-2bc73df230ab",
"title": "Articles",
"template": "articles.index",
"shares": 53,
"content": "<p>These are my various writings. Some might say they are cool.222</p>\n"
},
{
"id": "56f274ce-a723-4913-bae6-1d13833c356f",
"title": "Home",
"template": "home",
"shares": 99,
"content": "<p>I see a bad-ass mother who don't take no crap off of nobody.</p>\n"
},
{
"id": "62136fa2-9e5c-4c38-a894-a2753f02f5ff",
"title": "About",
"template": "page",
"shares": 54,
"content": "<p>I'm just a kid living in the 90's, writing articles in his secret public journal wonder if someday, somewhere in the future, they will be read by someone.</p>\n"
},
{
"id": "b9e4bfe3-9c12-4553-b7ef-f43c22ffaa63",
"title": "Articles",
"template": "articles.index",
"shares": 21,
"content": "<p>These are my various writings. Some might say they are cool.</p>\n"
},
{
"id": "f5a53468-017e-4725-b5b6-a745df7e94cb",
"title": "Topics",
"template": "topics/index",
"shares": 37,
"content": null
},
{
"id": "home",
"title": "Home",
"template": "home",
"shares": 98,
"content": "<p>I see a bad-ass mother who don't take no crap off of nobody.</p>\n"
}
],
Ah! I found the problem. I had accidentally updated the Statamic EntryQueryBuilder and not the eloquent-driver EntryQueryBuilder file.
Yes, this update resolved the issue, and I'm able to query the ID and slug fields without including the collection and all the other fields show up properly as well.
Thanks!