0

我正在尝试,send email three days before the expired date,但我不知道该怎么做?

逻辑

  1. 检索剩余三天到期的所有订阅者
  2. 向他们的用户发送电子邮件

代码

表我需要检查命名的时间戳subscribes.

$subscribes = Subscribe::all();

此表有一个名为的列expires_at,我需要检查它以查找3 days left.

还有我的邮件

Mail::to($user->email)->send(new SubscribeExpire($user, $subscribe));

我对这个碳计算的事情感到困惑,任何人都可以帮忙吗?

更新

根据下面的答案,我现在有了这个:

$subscribes = Subscribe::where('expires_at', Carbon::now()->subDays(3))->get();
        $user = [];
        $package = [];
        foreach($subscribes as $subscribe){
            $user = User::where('email', $subscribe->email);
            $package = Package::where('id', $subscribe->package_id);
        }

        Mail::to($user->email)->send(new SubscribeExpire($user, $package));

但是当我运行命令时会出现此错误

ErrorException  : Trying to get property 'email' of non-object
4

2 回答 2

1

使用subDays()方法,如下所示:

$subscribes = Subscribe::where('expires_at', Carbon::now()->subDays(3))->get();
于 2018-12-03T16:06:02.223 回答
1
  // here you get subscribes  
  // if you are going to send three days before the expiry date, this means we need to check if expires_at is in three days so probably need to add days. Or maybe even check if time left before expiration is more than three days and less than one day and run it once per day?
  $subscribes = Subscribe::whereDate('expires_at', Carbon::now()->addDays(3))->get();
    $user = [];
    $package = [];
    foreach($subscribes as $subscribe){
        // you probably have only one user with this email
        $user = User::where('email', $subscribe->email)->first();
        // you probably have one associated package
        $package = Package::where('id', $subscribe->package_id)->first();
    }
    // check if user and package are found
    if(is_object($user) && is_object($package)){
      Mail::to($user->email)->send(new SubscribeExpire($user, $package));
    }
于 2018-12-03T16:37:08.453 回答