0

how can i create a method in laravel in my USER controller that when i click on a button in the view it will download a txt file with this info tha its generating from this query

User::select('id','name','lastname')
            ->orderBy('id','desc')
            ->take(100)
            ->get();

Printing these 3 fields of Users table in 3 column in the txt file. If someone can guide me doing this via jquery-ajax it would be perfect !

UPDATED my problem is solved thanks to the guy below !

1

1 Answer 1

2
//controller

public function downloadTxt()
{
  $txt = "";
  $datas = User::select('id','name','lastname')
            ->orderBy('id','desc')
            ->take(100)
            ->get();
  foreach($datas as $data){
  $txt .= $data['id'].'|'.$data['name'].'|'.$data['lastname'].PHP_EOL;
  }
  $txtname = 'mytxt.txt';
     $headers = ['Content-type'=>'text/plain', 'test'=>'YoYo', 'Content-Disposition'=>sprintf('attachment; filename="%s"', $txtname),'X-BooYAH'=>'WorkyWorky','Content-Length'=>sizeof($datas)];
        return \Response::make($txt , 200, $headers );

}
Sign up to request clarification or add additional context in comments.

2 Comments

i need all of them to show them and in three columns if thats possible
Non-ISO extended-ASCII text => can i do this format in my file ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.