Ad

Laravel Queue On Repository

- 1 answer

I am using laravel as api for angular js app and i have a newsletter api call and the success of that call takes more time so i thought to use queue system , created a queue job and I can access the queue from controller , but i can not access from repository

namespace App\Jobs;

use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendStudentRequirement extends Job implements SelfHandling,ShouldQueue
{
   use InteractsWithQueue, SerializesModels;
   protected $data;

   public function __construct($data)
   {
      $this->data = $data;
   }

   public function handle()
   {
    file_put_contents('test.txt', $this->data);
   }
}

When i call $this->dispatch(new SendStudentRequirement('data test')); from controller its working fine , but from repository its throwing error as undefined SendStudentRequirement.

Ad

Answer

You should use queues: https://laravel.com/docs/5.2/queues

I dont know which provider you use, but there some built in email providers and even if you have an other one that is not a problem.

Simply implement a message queue and handler that will send your emails, so the response for the client should be quick

Ad
source: stackoverflow.com
Ad