you're definitely going to crash your application if you are trying to output that many rows at once.
You should look at writing your csv in 'chunks'.
Look at the following for selecting with chunks. https://laravel.com/docs/9.x/queries#chunking-results
and an example (not very new) of getting chunked data into a csv https://barryvdh.nl/laravel/2015/02/07/streaming-large-csv-files-with-laravel-chunk/