Home  >  Q&A  >  body text

Importing large CSV files using cron jobs in Laravel

<p>I'm using <strong>Laravel 8</strong> and I want to <strong>update billions of product prices</strong>. I added this <strong>code and it worked fine</strong> but it was <strong>not efficient</strong> and it increased the <strong>load on the server</strong> . </p> <pre class="brush:php;toolbar:false;">try { $priceCsvs = PriceCsv::whereStatus(PriceCsv::PENDING)->get(); foreach ($priceCsvs as $price) { dump($price->name." is started"); $csvData = fopen($price->file_url, 'r'); $firstline = true; while (($data = fgetcsv($csvData, 555, ',')) !== false) { if (!$firstline && !empty($data)) { dump($data); } $firstline = false; } fclose($csvData); dump($price->name." is End"); } } catch (\Exception $ex) { dump($ex->getMessage()); }</pre> <p><em><strong>The point is: </strong></em>Is there a way to get the input from a <strong>CSV file</strong> or any other file<strong> Add sleep after 1000 times</strong>What works. </p>
P粉024986150P粉024986150382 days ago460

reply all(1)I'll reply

  • P粉242126786

    P粉2421267862023-09-03 10:50:36

    If you are looking for a placement job, check out

    Task Scheduling

    Basically define some console (artisan) commands and then schedule them in a console kernel file. The scheduler is run using artisan and it is recommended to run the command (define cron job) every minute. If the kernel schedule definition is consistent with the minute the "php artisan Schedule:work" command is run, then your job will execute.

    reply
    0
  • Cancelreply