Why do you use SerializesModels trait when you are not using models in your job?
Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Models\Model_name] inside laravel JOB
I am trying to dump data from csv to database. But, I am getting an error as ModelNotFound inside laravel job. Although I have already imported model.
Error
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}
My JobFile
<?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);
}
}
}
Skip and map data are just an array used to determine which column from csv map to which column in database
In your constructor, you instantiate two services. These are serialized and will be attempted to be unserialized by the SerializesModels trait - but it will not know how to unserialize this:
$this->dbase = new Colorado_voter();
since what the SerializesModels trait does is save the model primary key on serialize and fetch it out of the database on unserialize. However, your Colorado_voter() does not have a value for its primary key since it is not yet saved to the database.
So I think my question should be: Why do you instantiate these classes in the constructor when you could do it in the handle method as they do not rely on parameters sent to the constructor.
Please or to participate in this conversation.