I tried to create a job in order to send emails to all users in the database I have done everything and connected successfully with Mailtrip but still have a problem : (Failed) when implementing the command:
PHP artisan queue:work
this is my ProductEmail class:
class ProductMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
public $product;
public $user;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Product $product, User $user)
{
$this->product = $product;
$this->user = $user;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject($this->product->name)
->view('email.product');
}
}
and i have created the view for it>>>>
and here the job class
class NotifyUsersForProduct implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public $product;
public function __construct(Product $product)
{
$this->$product = $product;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$users = User::all();
$users->map(function(User $user){
Mail::to($user)->send(new ProductMail($this->product, $user));
});
}
}
and here i have use it
try {
$product = Product::create([
'name' => $request->input('name'),
'price' => $request->input('price'),
'quantity' => $request->input('quantity'),
'user_id' => Auth::id(),
]);
NotifyUsersForProduct::dispatch($product);
CodePudding user response:
public function __construct(Product $product)
{
$this->$product = $product;
}
This should be:
public function __construct(Product $product)
{
$this->product = $product;
}
Remove $ and also i recommend to do this but not necessary:
public function handle()
{
$users = User::all();
foreach($users as $user){
Mail::to($user)->send(new ProductMail($this->product, $user));
});
}
Hope it helps.