ssquare
10 months ago
164
4
Laravel

SQLSTATE[42S02]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid object name 'failed_jobs'.

Posted 10 months ago by ssquare

I have a demo csv file and I want to dump this data from csv file to database using laravel jobs. But I am receiving the following error.

[2018-12-11 05:46:23] local.ERROR: SQLSTATE[42S02]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid object name 'failed_jobs'. (SQL: insert into [failed_jobs] ([connection], [queue], [payload], [exception], [failed_at]) values (database, default, {"displayName":"App\Jobs\StoreColorado","job":"Illuminate\Queue\[email protected]","maxTries":null,"timeout":null,"timeoutAt":null,"data":{"commandName":"App\Jobs\StoreColorado","command":"O:22:\"App\Jobs\StoreColorado\":10:{s:31:\"\u0000App\Jobs\StoreColorado\u0000service\";O:29:\"App\Http\Service\KabzaService\":0:{}s:34:\"\u0000App\Jobs\StoreColorado\u0000table_name\";N;s:29:\"\u0000App\Jobs\StoreColorado\u0000dbase\";O:45:\"Illuminate\Contracts\Database\ModelIdentifier\":4:{s:5:\"class\";s:25:\"App\Models\Colorado_voter\";s:2:\"id\";N;s:9:\"relations\";a:0:{}s:10:\"connection\";N;}s:6:\"\u0000*\u0000job\";N;s:10:\"connection\";N;s:5:\"queue\";N;s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:5:\"delay\";O:13:\"Carbon\Carbon\":3:{s:4:\"date\";s:26:\"2018-12-11 05:46:21.085543\";s:13:\"timezone_type\";i:3;s:8:\"timezone\";s:3:\"UTC\";}s:7:\"chained\";a:0:{}}"}}, Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Models\Colorado_voter]. in D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php:414
Stack trace:
#0 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\SerializesAndRestoresModelIdentifiers.php(85): Illuminate\Database\Eloquent\Builder->firstOrFail()
#1 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\SerializesAndRestoresModelIdentifiers.php(55): App\Jobs\StoreColorado->restoreModel(Object(Illuminate\Contracts\Database\ModelIdentifier))
#2 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\SerializesModels.php(45): App\Jobs\StoreColorado->getRestoredPropertyValue(Object(Illuminate\Contracts\Database\ModelIdentifier))
#3 [internal function]: App\Jobs\StoreColorado->__wakeup()
#4 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\CallQueuedHandler.php(42): unserialize('O:22:"App\Jobs\...')
#5 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Jobs\Job.php(83): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\DatabaseJob), Array)
#6 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(326): Illuminate\Queue\Jobs\Job->fire()
#7 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(276): Illuminate\Queue\Worker->process('database', Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Illuminate\Queue\WorkerOptions))
#8 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(118): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\DatabaseJob), 'database', Object(Illuminate\Queue\WorkerOptions))
#9 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Console\WorkCommand.php(101): Illuminate\Queue\Worker->daemon('database', 'default', Object(Illuminate\Queue\WorkerOptions))
#10 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Console\WorkCommand.php(85): Illuminate\Queue\Console\WorkCommand->runWorker('database', 'default')
#11 [internal function]: Illuminate\Queue\Console\WorkCommand->handle()
#12 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(29): call_user_func_array(Array, Array)
#13 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#14 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#15 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Container\Container.php(564): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#16 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Console\Command.php(179): Illuminate\Container\Container->call(Array)
#17 D:\wamp64\www\kabza\vendor\symfony\console\Command\Command.php(255): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#18 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Console\Command.php(166): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#19 D:\wamp64\www\kabza\vendor\symfony\console\Application.php(886): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#20 D:\wamp64\www\kabza\vendor\symfony\console\Application.php(262): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#21 D:\wamp64\www\kabza\vendor\symfony\console\Application.php(145): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#22 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Console\Application.php(89): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#23 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(122): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#24 D:\wamp64\www\kabza\artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#25 {main}, 2018-12-11 05:46:23.888)) {"exception":"[object] (Illuminate\Database\QueryException(code: 42S02): SQLSTATE[42S02]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid object name 'failed_jobs'. (SQL: insert into [failed_jobs] ([connection], [queue], [payload], [exception], [failed_at]) values (database, default, {\"displayName\":\"App\\Jobs\\StoreColorado\",\"job\":\"Illuminate\\Queue\\[email protected]\",\"maxTries\":null,\"timeout\":null,\"timeoutAt\":null,\"data\":{\"commandName\":\"App\\Jobs\\StoreColorado\",\"command\":\"O:22:\\"App\\Jobs\\StoreColorado\\":10:{s:31:\\"\u0000App\\Jobs\\StoreColorado\u0000service\\";O:29:\\"App\\Http\\Service\\KabzaService\\":0:{}s:34:\\"\u0000App\\Jobs\\StoreColorado\u0000table_name\\";N;s:29:\\"\u0000App\\Jobs\\StoreColorado\u0000dbase\\";O:45:\\"Illuminate\\Contracts\\Database\\ModelIdentifier\\":4:{s:5:\\"class\\";s:25:\\"App\\Models\\Colorado_voter\\";s:2:\\"id\\";N;s:9:\\"relations\\";a:0:{}s:10:\\"connection\\";N;}s:6:\\"\u0000*\u0000job\\";N;s:10:\\"connection\\";N;s:5:\\"queue\\";N;s:15:\\"chainConnection\\";N;s:10:\\"chainQueue\\";N;s:5:\\"delay\\";O:13:\\"Carbon\\Carbon\\":3:{s:4:\\"date\\";s:26:\\"2018-12-11 05:46:21.085543\\";s:13:\\"timezone_type\\";i:3;s:8:\\"timezone\\";s:3:\\"UTC\\";}s:7:\\"chained\\";a:0:{}}\"}}, Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Models\Colorado_voter]. in D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php:414
Stack trace:
#0 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\SerializesAndRestoresModelIdentifiers.php(85): Illuminate\Database\Eloquent\Builder->firstOrFail()
#1 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\SerializesAndRestoresModelIdentifiers.php(55): App\Jobs\StoreColorado->restoreModel(Object(Illuminate\Contracts\Database\ModelIdentifier))
#2 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\SerializesModels.php(45): App\Jobs\StoreColorado->getRestoredPropertyValue(Object(Illuminate\Contracts\Database\ModelIdentifier))
#3 [internal function]: App\Jobs\StoreColorado->__wakeup()
#4 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\CallQueuedHandler.php(42): unserialize('O:22:\"App\\Jobs\\...')
#5 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Jobs\Job.php(83): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\DatabaseJob), Array)
#6 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(326): Illuminate\Queue\Jobs\Job->fire()
#7 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(276): Illuminate\Queue\Worker->process('database', Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Illuminate\Queue\WorkerOptions))
#8 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(118): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\DatabaseJob), 'database', Object(Illuminate\Queue\WorkerOptions))
#9 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Console\WorkCommand.php(101): Illuminate\Queue\Worker->daemon('database', 'default', Object(Illuminate\Queue\WorkerOptions))
#10 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Console\WorkCommand.php(85): Illuminate\Queue\Console\WorkCommand->runWorker('database', 'default')
#11 [internal function]: Illuminate\Queue\Console\WorkCommand->handle()
#12 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(29): call_user_func_array(Array, Array)
#13 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#14 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#15 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Container\Container.php(564): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#16 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Console\Command.php(179): Illuminate\Container\Container->call(Array)
#17 D:\wamp64\www\kabza\vendor\symfony\console\Command\Command.php(255): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#18 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Console\Command.php(166): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#19 D:\wamp64\www\kabza\vendor\symfony\console\Application.php(886): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#20 D:\wamp64\www\kabza\vendor\symfony\console\Application.php(262): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#21 D:\wamp64\www\kabza\vendor\symfony\console\Application.php(145): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#22 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Console\Application.php(89): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#23 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(122): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#24 D:\wamp64\www\kabza\artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#25 {main}, 2018-12-11 05:46:23.888)) at D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664, Doctrine\DBAL\Driver\PDOException(code: 42S02): SQLSTATE[42S02]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid object name 'failed_jobs'. at D:\wamp64\www\kabza\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:144, PDOException(code: 42S02): SQLSTATE[42S02]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid object name 'failed_jobs'. at D:\wamp64\www\kabza\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:142, Illuminate\Database\Eloquent\ModelNotFoundException(code: 0): No query results for model [App\Models\Colorado_voter]. at D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php:414)
[stacktrace]
#0 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Database\Connection.php(624): Illuminate\Database\Connection->runQueryCallback('insert into [fa...', Array, Object(Closure))
#1 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Database\Connection.php(459): Illuminate\Database\Connection->run('insert into [fa...', Array, Object(Closure))
#2 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Database\Connection.php(411): Illuminate\Database\Connection->statement('insert into [fa...', Array)
#3 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Database\Query\Processors\SqlServerProcessor.php(24): Illuminate\Database\Connection->insert('insert into [fa...', Array)
#4 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(2494): Illuminate\Database\Query\Processors\SqlServerProcessor->processInsertGetId(Object(Illuminate\Database\Query\Builder), 'insert into [fa...', Array, NULL)
#5 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Failed\DatabaseFailedJobProvider.php(62): Illuminate\Database\Query\Builder->insertGetId(Array)
#6 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Console\WorkCommand.php(188): Illuminate\Queue\Failed\DatabaseFailedJobProvider->log('database', 'default', '{\"displayName\":...', 'Illuminate\\Data...')
#7 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Console\WorkCommand.php(137): Illuminate\Queue\Console\WorkCommand->logFailedJob(Object(Illuminate\Queue\Events\JobFailed))
#8 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php(360): Illuminate\Queue\Console\WorkCommand->Illuminate\Queue\Console\{closure}(Object(Illuminate\Queue\Events\JobFailed))
#9 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php(209): Illuminate\Events\Dispatcher->Illuminate\Events\{closure}('Illuminate\\Queu...', Array)
#10 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\FailingJob.php(36): Illuminate\Events\Dispatcher->dispatch('Illuminate\\Queu...')
#11 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\CallQueuedHandler.php(131): Illuminate\Queue\FailingJob::handle('database', Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Illuminate\Database\Eloquent\ModelNotFoundException))
#12 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\CallQueuedHandler.php(45): Illuminate\Queue\CallQueuedHandler->handleModelNotFound(Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Illuminate\Database\Eloquent\ModelNotFoundException))
#13 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Jobs\Job.php(83): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\DatabaseJob), Array)
#14 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(326): Illuminate\Queue\Jobs\Job->fire()
#15 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(276): Illuminate\Queue\Worker->process('database', Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Illuminate\Queue\WorkerOptions))
#16 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(118): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\DatabaseJob), 'database', Object(Illuminate\Queue\WorkerOptions))
#17 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Console\WorkCommand.php(101): Illuminate\Queue\Worker->daemon('database', 'default', Object(Illuminate\Queue\WorkerOptions))
#18 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Queue\Console\WorkCommand.php(85): Illuminate\Queue\Console\WorkCommand->runWorker('database', 'default')
#19 [internal function]: Illuminate\Queue\Console\WorkCommand->handle()
#20 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(29): call_user_func_array(Array, Array)
#21 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#22 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#23 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Container\Container.php(564): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#24 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Console\Command.php(179): Illuminate\Container\Container->call(Array)
#25 D:\wamp64\www\kabza\vendor\symfony\console\Command\Command.php(255): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#26 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Console\Command.php(166): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#27 D:\wamp64\www\kabza\vendor\symfony\console\Application.php(886): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#28 D:\wamp64\www\kabza\vendor\symfony\console\Application.php(262): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#29 D:\wamp64\www\kabza\vendor\symfony\console\Application.php(145): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#30 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Console\Application.php(89): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#31 D:\wamp64\www\kabza\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(122): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#32 D:\wamp64\www\kabza\artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#33 {main}
"} 


My Jobs file looks like

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

use League\Csv\Reader;
use League\Csv\Statement;

use App\Http\Service\KabzaService;
use App\Models\Colorado_voter;
use App\Models\Base_voter;

class StoreColorado implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */

    private $service;
    private $table_name;
    private $dbase;

    public function __construct() {  
        $this->service = new KabzaService();
        $this->table = 'colorado_voter';
        $this->dbase = new Colorado_voter();
    }
 
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $offset = 0;
        $limit = 50;
        $filename = '1000.csv';

        $skip = array
        (
            8 => 1,
            25 => 1,
            26 => 1,
            27 => 1,
            31 => 1,
            44 => 1,
            49 => 1
        );
        $map_data = array
        (
            0 => 'voter_id',
            1 => 'voter_county_code',
            2 => 'voter_county',
            3 => 'name_last',
            4 => 'name_first',
            5 => 'name_middle',
            6 => 'name_suffix',
            7 => 'name_voter',
            8 => 'id',
            9 => 'voter_precinct_name',
            10 => 'voter_library_id',
            11 => 'home_house_number',
            12 => 'home_house_suffix',
            13 => 'home_direction',
            14 => 'home_street_name',
            15 => 'home_street_type',
            16 => 'home_direction',
            17 => 'home_unit_type',
            18 => 'home_unit_number',
            19 => 'home_street_address_1',
            20 => 'home_address_city',
            21 => 'home_address_state',
            22 => 'home_address_zip',
            23 => 'home_address_zip_plus',
            24 => 'home_address_effective_date',
            25 => 'id',
            26 => 'id',
            27 => 'id',
            28 => 'birth_year',
            29 => 'gender',
            30 => 'voter_precinct_name',
            31 => 'id',
            32 => 'voter_status_id',
            33 => 'voter_party',
            34 => 'voter_party_affiliation_date',
            35 => 'phone_preferred',
            36 => 'mailing_street_address_1',
            37 => 'mailing_street_address_2',
            38 => 'mailing_street_address_3',
            39 => 'mailing_address_city',
            40 => 'mailing_address_state',
            41 => 'mailing_address_zip',
            42 => 'mailing_address_zip_plus',
            43 => 'mailing_address_country',
            44 => 'id',
            45 => 'permanent_mail_in_voter',
            46 => 'voter_congressional_district',
            47 => 'voter_state_district',
            48 => 'voter_house_district',
            49 => 'id'
        );

        $db_header_obj = new Colorado_voter();
        $db_header = $db_header_obj->getTableColumns();

        $base_voter_header = new Base_voter();
        $base_voter_columns = $base_voter_header->getTableColumns();

        $csv_file_path = storage_path('app/files/colorado_voter/').$filename;
        if (!ini_get("auto_detect_line_endings")) {
            ini_set("auto_detect_line_endings", TRUE);
        }
        $csv = Reader::createFromPath($csv_file_path, 'r');

        $csv->setOutputBOM(Reader::BOM_UTF8);
        $csv->addStreamFilter('convert.iconv.ISO-8859-15/UTF-8');

            $csv->setHeaderOffset(0); //set the CSV header offset
            $csv_header = $csv->getHeader();    

            $rec_arr = array();
            $records = array();
            $records_arr = array();

            $stmt = (new Statement())
            ->offset($offset)
            ->limit($limit)
            ;

            $records = $stmt->process($csv);

            foreach ($records as $record) 
            {
                $rec_arr[] = array_values($record);
            }

            $records_arr = $this->service->trimArray($rec_arr);

            print_r($records_arr);

            if (!ini_get("auto_detect_line_endings")) {
                ini_set("auto_detect_line_endings", FALSE);
            }
        }


}

I am using CSV league to parse csv file. Those skip and map_data are just the array which will be used later to map which column from csv to which column in database and skip will be used to skip some columns.

Please sign in or create an account to participate in this conversation.