Hi, I use the crud json api for my json api, it works great so far. Now I’d like to crate a custom method which only returns a few fields (id, name) for a certain table. Therefore I created a new function called slimlist. When I return the data like this: $this->set([ 'success' => true, 'data' => $query, '_serialize' => ['success', 'data'] ]);
I receive the json data just fine. But I’d like to receive the data in the same structure (Json Api style) as I do when I use the regular index functions of the Crud Json Api plugin. So my question is, how can I create a function that returns json api styled results but with limited fields?
Thanks
I don’t know if I understood your question, but if you are looking to get a JSON response from your controller you can echo it out. You have to pass your variables first like so…
$this->set(compact($var1, $var2));
Then you have to make sure you JSON encode them like so…
echo json_encode(compact('var1', 'var2'));
I hope I understood your question.
Happy coding !!
Hi,
You can use CRUD API sparse fieldsets in your GET request to achieve filtered fields.
thank you for your answers, the sparse fieldsets did it for me!