Laravel returning a leading \n character on responses

First of all PHEWW! this is a bad issue to end up with.

This can happen when any of your files starting from index.php of public folder to the controller which throws the response. Any where if at all there is a <?php ?> with a new line in between

or

any file involved in route request and response starting with <?php is on any other line than line no.1

Check your

  1. public/index.php
  2. bootstrap/app.php
  3. config/app.php
  4. any other files under config folder
  5. routes/web.php or routes/api.php
  6. app/Http/Controllers/YourController.php

I feel checking these must fix your issue. if not comment below lets discuss!


i was getting the same issue and used the following command to see what is in the buffer before returning any data

dd(ob_get_contents());

it turned out that i have several "\n" in the (routes.php) or in the routes files in case you use multiple files


I ran into the same issue and fixed it with phpStorm's global regex search:

[\s\S]\<\?php

In the end I had a new line in the index.php before <?php like mentioned in the other comments

Tags:

Laravel