I am trying to dispatch my send email action using Laravel database
queue
however this process still continues in my browser instead of working behind.
this is my controller
protected function importUserExcel(UploadedFile $file, Request $request){
$user_role = Role::where('name','=','user')->first();
\Excel::load($file, function($reader) use ($user_role) {
$excel = $reader->select()->get();
foreach($excel[0] as $line){
$user = User::firstOrnew([
'email' => $line['email']]);
$user->email = $line['email'];
$user->name = $line['name'];
$user->password= bcrypt(srand(15));
$user->town = $line['town'];
$user->dealer_code = $line['dealer_code'];
$user->type = $line['type'];
// $user->save();
$user->sendUserEmail();
//$user->attachRole($user_role);
}
});
}
this is my model function
public function sendUserEmail()
{
$delay = Carbon::now()->addMinutes(15);
\Log::info("Request Begins");
$user = new SendEmails($this);
$user->delay($delay);
dispatch($user);
\Log::info("Request Ends");
}
and this is my job
class SendEmails implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->handle($user);
}
/**
* Execute the job.
*
* @return void
*/
public function handle(User $user)
{
$broker = $user->broker;
$brokerInstance = \Password::broker($broker);
view()->share('locale', app()->getLocale());
$response = $brokerInstance->sendResetLink([ 'email' => $user->email ], function (Message $message) {
$message->subject(trans('emails.welcome_subject'));
});
}
}
however result seems coming eventually not delaying or queueing anything. Meanwhile my browser also process instead of putting process to behind.
SendEmails
class. If you push Logger into your model, you could not see the delay time. By the way, in your controller functions should return View to send the response to browser – Jonny Vu