Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thyrizwan's avatar

Efficient bulk data insertion (250K records) in Laravel without exceeding PHP time limit? (Queue, multi-tenancy)

I'm encountering a PHP execution time error while inserting approximately 250k records into a MySQL database using Laravel Eloquent. I'm aiming to achieve this without increasing the overall execution time, considering the following constraints:

  1. Frontend: Angular
  2. Multi-tenancy: Implemented using tenancy for laravel
  3. Attempted solutions: Laravel Queue

Below is my code.

Job File Code -

class MedicineData implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Batchable;

    public $data;

    public function __construct($data)
    {
        $this->data = $data;

    }

    public function handle(): void
    {
        foreach ($this->data as $key => $value) {
            $medicine = new MedicineName();
            $medicine->name = $value['name'];
            $medicine->save();
        }
    }

}

Controller File Code

$chunks = array_chunk($data, 500);
$batch = Bus::batch([])->dispatch();

foreach ($chunks as $key => $value) {
    $batch->add(new MedicineData($value));
}

I am getting an error of Maximum PHP Execution Time. How to resolve this.

0 likes
0 replies

Please or to participate in this conversation.