Reputation: 19
this is the first time I am using laravel queue jobs, and somehow i could not get it working.
This is my mail class:
class TopluKabulMektubu extends Mailable
{
use Queueable, SerializesModels;
public $letter;
public function __construct(AcceptLetter $letter)
{
$this->letter = $letter;
}
public function build()
{
$letter = $this->letter;
return $this->subject('Mail Title')
->view('emails.topluKabulSon')
->attach(public_path($letter->pdf), [
'as' => 'AcceptanceLetter.pdf',
'mime' => 'application/pdf',
]);
}
}
And I created a function inside my AcceptanceLetter model to use mail easier :
public function sendAcceptanceLetter(){
Mail::to('******@gmail.com')->queue(new TopluKabulMektubu($this));
if(Mail::failures()){
$this->email_send = 2;
$this->save();
}else{
$this->email_send = 1;
$this->save();
}
}
I created a queue table with php artisan queue:table and migrated, also changed queue connection to database from env file.
And my job file:
class QueueJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $letter;
public function __construct($letter)
{
//
$this->letter = $letter;
}
public function handle()
{
$this->letter->sendAcceptanceLetter();
}
}
Web route triggers my job :
Route::get('/topluDeneme', [PaginationController::class, 'topluQueue']);
And the controller:
public function topluQueue(){
$letters = AcceptLetter::where('email', '!=', null)->where('passport_number','!=','0');
foreach($letters as $letter){
QueueJob::dispatch($letter);
}
}
I expect when i run php artisan queue:listen on terminal and go to /topluDeneme route, mails to be sent. But nothing happens on terminal, mails not sent and nothing changes on job datatable.
Upvotes: 0
Views: 200
Reputation: 19
I found whats wrong with my code. It seems I forgot to use get() in the controller, here is the correct version :
public function topluQueue(){
$letters = AcceptLetter::where('email', '!=', null)->where('passport_number','!=','0')->get();
foreach($letters as $letter){
QueueJob::dispatch($letter);
}
}
Upvotes: 0