代码之家  ›  专栏  ›  技术社区  ›  JsWizard

通过发送邮件和登录Laravel安排呼叫

  •  0
  • JsWizard  · 技术社区  · 8 年前

    当发生这种情况时,我希望用该用户的名称向管理员发送电子邮件。

    // 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();
        }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   JsWizard    8 年前
    //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
    

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