I have created the job file and I'm dispatching the mail through the controller using mailable it will create the entry in job table but after the few seconds it will transfer the record into the failed_jobs table and gives error model not found
Controller Dispatch method
if (!empty($emails) && isset($result->email)) {
dispatch(new SendNotificationEmailJob($emails, $result, 'WarehouseAddUpdate', array($subject, $request['number'], 'add')));
}
SendNotificationEmailJob Class
<?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 Illuminate\Support\Facades\Mail;
class SendNotificationEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $emails;
protected $result;
protected $mailClass;
protected $data;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($emails, $result, $mailClass, $data)
{
$this->emails = $emails;
$this->result = $result;
$this->mailClass = 'App\Mail\\'.$mailClass;
$this->data = $data;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
if ($this->result == null) {
if (!empty($this->emails)) {
$to = $this->emails[0];
unset($this->emails[0]);
}
}else{
if($this->result != null && isset($this->result->email)){
$to = $this->result->email;
if (($key = array_search($this->result->email, $this->emails)) !== false) {
unset($this->emails[$key]);
}
}
}
if (!empty($this->emails) && isset($result->email)) {
$email = new $this->mailClass($this->data);
Mail::to($to)->cc($this->emails)->send($email);
}
if (empty($emails) && isset($result->email)) {
$email = new $this->mailClass($this->data);
Mail::to($to)->send($email);
}
}
}
Mailable Class
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class WarehouseAddUpdate extends Mailable
{
use Queueable, SerializesModels;
/**
* The data object instance.
*
* @var data
*/
public $subject;
public $warehouse;
public $type;
public $oldstatus;
public $newstatus;
public $receiver;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($data)
{
$this->subject = $data[0];
$this->warehouse = $data[1];
$this->type = $data[2];
$this->oldstatus = $data[3] ?? null;
$this->newstatus = $data[4] ?? null;
$this->receiver = 'Manager';
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('mails.warehouseaddupdate')->subject($this->subject);
}
}
this is my SendNotificationEmail Class
<?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 Illuminate\Support\Facades\Mail;
class SendNotificationEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $emails;
protected $result;
protected $mailClass;
protected $data;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($emails, $result, $mailClass, $data)
{
$this->emails = $emails;
$this->result = $result;
$this->mailClass = 'App\Mail\\'.$mailClass;
$this->data = $data;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
if ($this->result == null) {
if (!empty($this->emails)) {
$to = $this->emails[0];
unset($this->emails[0]);
}
}else{
if($this->result != null && isset($this->result->email)){
$to = $this->result->email;
if (($key = array_search($this->result->email, $this->emails)) !== false) {
unset($this->emails[$key]);
}
}
}
if (!empty($this->emails) && isset($result->email)) {
$email = new $this->mailClass($this->data);
Mail::to($to)->cc($this->emails)->send($email);
}
if (empty($emails) && isset($result->email)) {
$email = new $this->mailClass($this->data);
Mail::to($to)->send($email);
}
}
}
The error I found in DB failed_jobs table
Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Model\V1\User]. in /var/www/html/smartcgd/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:416 Stack trace:
0 /var/www/html/smartcgd/vendor/laravel/framework/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php(54):
Illuminate\Database\Eloquent\Builder->firstOrFail()
1 /var/www/html/smartcgd/vendor/laravel/framework/src/Illuminate/Queue/SerializesModels.php(41):
App\Jobs\SendNotificationEmailJob->getRestoredPropertyValue(Object(Illuminate\Contracts\Database\ModelIdentifier))
2 [internal function]: App\Jobs\SendNotificationEmailJob->__wakeup()
3 /var/www/html/smartcgd/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(42):
unserialize('O:33:"App\Jobs\...')
4 /var/www/html/smartcgd/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(76):
Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\DatabaseJob), Array)
5 /var/www/html/smartcgd/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(320):
Illuminate\Queue\Jobs\Job->fire()
6 /var/www/html/smartcgd/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(270):
Illuminate\Queue\Worker->process('database', Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Illuminate\Queue\WorkerOptions))
7 /var/www/html/smartcgd/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(114):
Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\DatabaseJob), 'database', Object(Illuminate\Queue\WorkerOptions))
8 /var/www/html/smartcgd/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(101):
Illuminate\Queue\Worker->daemon('database', 'default', Object(Illuminate\Queue\WorkerOptions))
9 /var/www/html/smartcgd/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(85):
Illuminate\Queue\Console\WorkCommand->runWorker('database', 'default')
10 [internal function]: Illuminate\Queue\Console\WorkCommand->handle()
11 /var/www/html/smartcgd/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29):
call_user_func_array(Array, Array)
12 /var/www/html/smartcgd/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87):
Illuminate\Container\BoundMethod::Illuminate\Container{closure}()
13 /var/www/html/smartcgd/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31):
Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
14 /var/www/html/smartcgd/vendor/laravel/framework/src/Illuminate/Container/Container.php(549):
Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
15 /var/www/html/smartcgd/vendor/laravel/framework/src/Illuminate/Console/Command.php(183):
Illuminate\Container\Container->call(Array)
16 /var/www/html/smartcgd/vendor/symfony/console/Command/Command.php(252):
Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
17 /var/www/html/smartcgd/vendor/laravel/framework/src/Illuminate/Console/Command.php(170):
Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
18 /var/www/html/smartcgd/vendor/symfony/console/Application.php(946):
Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
19 /var/www/html/smartcgd/vendor/symfony/console/Application.php(248):
Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
20 /var/www/html/smartcgd/vendor/symfony/console/Application.php(148):
Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
21 /var/www/html/smartcgd/vendor/laravel/framework/src/Illuminate/Console/Application.php(88):
Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
22 /var/www/html/smartcgd/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(121):
Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
23 /var/www/html/smartcgd/artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput),
Object(Symfony\Component\Console\Output\ConsoleOutput))
24 {main}
1 Answer 1
If you use a model object within a Laravel job, the job relies on the object's ID to retrieve the corresponding model object. Laravel will display an error if the ID is not provided. Therefore, when passing an object to the job, ensure that the object's ID is included along with the object itself.
No query results for model
when you're usingfirstOrFail()
. This means it could not find the user.firstOrFail()
is not in this code, but is apparently triggered inApp\Jobs\SendNotificationEmailJob->__wakeup()
SendNotificationEmailJob->getRestoredPropertyValue
which I don't see here, either. You can also do a search forUser::firstOrFail
in your project to see where it might be called.