I failed to make the email queue. Not sure what is wrong as I keep getting the error.
‘ArgumentCountError: Too few arguments to function
App\Mail\TransactionSuccess::__construct()’.
What I’m trying to do is to pass the $trx
variable when sending emails.
TestController.php
class TestController extends Controller
{
public function sendtrx(){
$trx = Transaction::find('123');
dispatch(new TrxEmailJob($trx));
}
}
Jobs/TrxEmailJob.php
class TrxEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $trx;
public function __construct(Transaction $trx)
{
$this->trx = $trx;
// dd( $this->trx);
}
public function handle()
{
Mail::to($this->trx['user_email'])->send(new TransactionSuccess());
}
}
Mail/TransactionSuccess.php
class TransactionSuccess extends Mailable
{
use Queueable, SerializesModels;
public $trx;
public function __construct(Transaction $trx)
{
$this->trx = $trx;
}
public function build()
{
return $this->view('Hey Test')
->markdown('emails.transactionsuccess');
}
}
I expect to send an email with details from $trx; however, the job fails based on the table.
ArgumentCountError: Too few arguments to function
App\Mail\TransactionSuccess::__construct(), 0 passed in
/Users/hey/testcode/app/Jobs/TrxEmailJob.php on line 37 and exactly 1
expected in /Users/hey/testcode/app/Mail/TransactionSuccess.php:20
Stack trace:
>Solution :
Update the handle() method to the following.
public function handle()
{
Mail::to($this->trx['user_email'])
->send(new TransactionSuccess($this->trx));
}