dcblogdev / dcblogcomments

2 stars 0 forks source link

laravel-import-large-csv-file #19

Open utterances-bot opened 4 years ago

utterances-bot commented 4 years ago

Laravel import large CSV file - DC Blog

I recently had to import a 30,000 line CSV file, in this post, I document how I tackled this...

https://dcblog.dev/laravel-import-large-csv-file

Abhinavmule123 commented 4 years ago

data are inserting into database , can you tell why it is happen ?

Abhinavmule123 commented 4 years ago

it was only stored in pendingcontacts folder not inserting

dcblogdev commented 4 years ago

Do you have the 2nd console script to process the pendingcontacts?

Abhinavmule123 commented 4 years ago

No..Actually Data is not inserting in database just stored in pendingcontacts ,but i don't know why is not inserted in database.

dcblogdev commented 4 years ago

the first console script only stored the contacts into csv files, the second script is used to go through the csv files in insert them in batches.

Abhinavmule123 commented 4 years ago

But in my case 2nd script not work , only 1st script work it stored csv files but not inserting ..Can You send Me Full Source Code on abhinavmule123@gmail.com here.

dcblogdev commented 4 years ago

Ahh I misread this. There is only 1 console script.

This:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Contact;

class importContacts extends Command
{
    protected $data;

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'import:contacts';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Import contacts from an array.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $path = base_path("resources/pendingcontacts/*.csv");  
        foreach (array_slice(glob($path),0,2) as $file) {
            $data = array_map('str_getcsv', file($file));

            foreach($data as $row) {
                Contact::updateOrCreate([
                    'email' => $row[0],
                ], ['email' => $row[0]]);
            }

            unlink($file);
        }
    }
}

You may need to tweak the $row[0] parts to point to the right column of the CSV where the email address is stored. This example assumes the first column is the email address.

git-raju commented 4 years ago

This script works perfectly when data is small like 10K-15K rows in csv file. I am trying to import a csv data which have 10 million rows in Laravel, it throws s post max size issue. I just want to upload huge csv without altering any PHP ini setting. Any help?

Thanks.

memyselfaashish commented 4 years ago

Everything went fine. The artisan command works perfectly when I do it manually. Also, the scheduler pushes the command to run (found out by log entries), but doesn't make any database entries.

What is happening here?

php artisan schedule:run throws this:

Running scheduled command: "C:\xampp\php\php.exe" "artisan" import:questions > "NUL" 2>&1

and Laravel Version is 5.x.x

dongosiddik commented 3 years ago

Good evening sir, really thank you for this tutorial. I cannot insert into database. files are always stored in the "resources / pendingcontacts" file. Help me correct the problem. thank you

azhar62355 commented 1 year ago

can anybody tell me how i can store data from resource pendingcontact folder to mysql database