title: Generating and downloading a text file on the fly in Laravel
body: <p>My data is stored in the database and I need to give it out of it in a text file. There is no point in creating a file on disk, but an attempt to generate a response with a file does not work. Found a way like this but it doesn't work <code>ERR_INVALID_RESPONSE</code></p>
  <pre><code> $contents = 'My data from db';
       
     $filename = 'example.txt';
  
     return response()-&gt;streamDownload(function () use ($contents) {
           echo $contents;
       }, $filename);
  </code></pre>