0

I know that any array response will be converted to json in laravel by default, but i was wondering if there is a way to return the response as csv without installing any third-party library.

I need to make one controller action returns a csv instead of JSON.

Thanx for your advice,

Ya Basha
  • 1,902
  • 6
  • 30
  • 54
  • 1
    It isn't clear if you mean always return CSV, or for a particular method. Laravel is just PHP - generate your CSV with `fputcsv()` as you normally would, echo the right Content-Type header, and echo the CSV. There are many examples here, here's a good one: https://stackoverflow.com/questions/16251625/how-to-create-and-download-a-csv-file-from-php-script – Don't Panic May 13 '20 at 22:58

1 Answers1

1

You can't do that without writing a lot of code.

Take a look at this code which is the minimum you will have to do to achieve it.

Or just install the package and enjoy.

MrEduar
  • 1,784
  • 10
  • 22