首页 > 解决方案 > 使用发送邮件安排通话并登录 Laravel

问题描述

如果某些用户的“deleted_at”字段在日常检查时为“null”,如何制作通知消息?发生这种情况时,我希望向管理员发送一封带有该用户名的电子邮件。

// app/console/Kernel.php

    protected function schedule(Schedule $schedule)
    {
        $schedule->call(function () {
            $forgotCheckout = DB::table('workings')->whereNull('deleted_at')->get();
            foreach($forgotCheckout as $f){
                $user_id = [$f->user_id];
            }
            -- "code" --
        })->daily();
    }

标签: phplaravel

解决方案


//app/console/Kernel.php

use Illuminate\Support\Facades\Mail; //for use Mail facade

protected function schedule(Schedule $schedule)
{
    $forgot = [];

    $forgotCheckout = Working::whereNull('deleted_at')->get();
    foreach($forgotCheckout as $forgot){
           $forgot;
       }

    if(!is_null($forgot)){
        $schedule->call(function () use($forgotCheckout){
            Mail::send( //send email with valiable $forgot in View file.
                'emails.forgot_checkout',
                compact('forgotCheckout'),
                function ($message) {
                    $message->to('test@email.com');
                    $message->subject('This is test mail');
                }
               );
        })->daily()->when(function() use ($forgotCheckout){ //define how often do this job
            if(!is_null($forgot)){
            \Log::info(Daily check completed.); //write log file                
            return true; // when() will work when return is true.
            }
            else {
            return false; // when() not work because return is false.
            }
        );
    }
}

您可以制作和修改以将您的数据与$forgot数据一起使用。

// view/emails/forgot_checkout.blade.php

This is test mail for scheduled mail sending...
@foreach($forgotCheckout as $forgot)
  {{$forgot->id}}
  {{$forgot->name}}
@endforeach

大多数人都知道,如果您知道该怎么做,那就不难了:)


推荐阅读