0

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}

asked May 31, 2019 at 12:23
5
  • It's not giving model not found, it's giving No query results for model when you're using firstOrFail(). This means it could not find the user. firstOrFail() is not in this code, but is apparently triggered in App\Jobs\SendNotificationEmailJob->__wakeup() Commented May 31, 2019 at 12:50
  • but I'have not used the firstOrFail() in my model and also that this dispatch code is working fine with another model purchaseorder Commented May 31, 2019 at 13:20
  • It probably does work fine for the purchase order because it can find the record. But it's not finding the record for User in this one. Can you show the __wakeup() function that exists in either SendNotificationEmailJob or ShouldQueue? Commented May 31, 2019 at 13:26
  • I have added SendNotificationEmailJob class please check, and I checked __wakeup() function in my entire project that is only found in vendor files with default code I have not changed the signle line there. BTW thanks for doing such a efforts Commented Jun 3, 2019 at 4:51
  • Whoops, I missed a line. It seems to be called from SendNotificationEmailJob->getRestoredPropertyValue which I don't see here, either. You can also do a search for User::firstOrFail in your project to see where it might be called. Commented Jun 3, 2019 at 12:34

1 Answer 1

0

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.

answered May 15, 2023 at 10:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.