1

Im using Dingo API with Laravel/Lumen. and so far im getting the correct HTTP responses and data.

However I want to be able to format the way Dingo returns its JSON string to JSON_PRETTY_PRINT

Currently its return JSON in the following format:

{"data":[{"id":1},{"id":2},{"id":3},{"id":4},{"id":5}]}

i want it to be outputted as such:

    {
      "data":[
         {
           "id":1
         },
         {
           "id":2
         },
         {
           "id":3
         },
         {
           "id":4
         },
         {
           "id":5
         }
      ]
   }

My controller file:

class ArticleController extends ApiController
{
    public function index(Manager $manager, ArticleTransformer $articleTransformer)
    {
        $articles       = Article::take(5)->get();
        $collection     = new Collection($articles, $articleTransformer);
        $data           = $manager->createData($collection)->toArray();

        return reponse()->json($data, 200, array, JSON_PRETTY_PRINT);
    }
}

My Transformer File:

class ArticleTransformer extends TransformerAbstract
{
    public function transform(Article $article)
    {
        return [
            'id'    => (int) $article->id
        ];
    }

    public function getDefaultIncludes()
    {

    }
}
  • Hi there. I don't think you can get such as output from json server. They removes white spaces automatically. So you have to format it, after you got from server if you like to print it in pretty format. – tanaydin Jun 14 '16 at 09:48
  • oh really? i wasn't aware of that. thanx for the response. –  Jun 14 '16 at 09:49

0 Answers0