Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Dispatching a job not working from a repository

when I try to dispatch a job from a controller it works.

however when I do the same from a repository it gives me an error.

<?php

namespace App\Repositories\Retailer;

use App\Jobs\SlackJob;
use App\Traits\CreateOrderTrait;

class CreateOrderRepo
{
   use CreateOrderTrait;

   public function create($store_id)
     {
        $slackJob = new SlackJob("Test", 1);
        $slackJob = $slackJob->onQueue('high');
        $this->dispatch($slackJob);
     }
}

the error:

Call to undefined method App\Repositories\Retailer\CreateOrderRepo::dispatch()

like image 455
Garine Avatar asked Oct 12 '18 14:10

Garine


2 Answers

Add the trait to dispatch the jobs:

use Illuminate\Foundation\Bus\DispatchesJobs;
like image 179
Paras Avatar answered Nov 16 '22 14:11

Paras


change this

$this->dispatch($slackJob);

to this

dispatch($slackJob);
like image 1
wheelmaker Avatar answered Nov 16 '22 12:11

wheelmaker