Reputation: 31
I got a huge 500m file or even greater, i need to parse the text and insert data from the file into db.I know how to accomplish it using plain php (read line by line, or by small chunks).The question is: Is it possible to accomplish using laravel filesystem abstraction (laravel uses "thephpleague flysystem" library).Thanks!
Upvotes: 1
Views: 1508
Reputation: 46
To read files line by line in Laravel you can use File::lines('file_test.txt') which use LazyCollection or use LazyCollection directly.
Example:
use Illuminate\Support\Facades\File;
foreach (File::lines('file_test.txt') as $line) {
//Modifications in current line
}
References:
https://laravel.com/api/9.x/Illuminate/Support/Facades/File.html#method_lines
https://laravel.com/docs/9.x/collections#lazy-collections
Upvotes: 0
Reputation: 1041
It's on phpleague's documentation, you can use "chunk" method :
https://csv.thephpleague.com/9.0/connections/output/#outputting-the-document-into-chunks
Instead of output, you could insert data in database
Upvotes: 1