0

I want return the json data from my controller. I have written belo code.

$this->loadModel('Users');
$query = $this->Users->find();
$users = $query->select(['id', 'name']);
echo json_encode($users);

This return data in below format: [{"id":1,"news_type":1,"name":"hoge"},{"id":2,"news_type":1,"name":"hoge1"}]

but I want it in below format: {"categories":[{"id":1,"news_type":1,"name":"hoge"},{"id":2,"news_type":1,"name":"hoge1"}]}

Amol
  • 69
  • 2
  • 11
  • Do not echo data from controllers, that's not how controllers are supposed to behave! **https://stackoverflow.com/questions/42378793/how-to-output-custom-http-body-contents-with-cakephp-3-4-echoing-causes-unable** – ndm Jun 27 '17 at 13:29

1 Answers1

0

Found solution.

I have used this:

echo json_encode(array("users" => $users));
Amol
  • 69
  • 2
  • 11