Laravel 与 Carbon 的日期差异

Laravel Date Difference with Carbon

我正在尝试 send email three days before the expired date, 但我不确定如何做?

逻辑

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

代码

Table 我需要检查名为 subscribes.

的时间戳
$subscribes = Subscribe::all();

这个 table 有一个名为 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

使用subDays()方法如下图:

$subscribes = Subscribe::where('expires_at', Carbon::now()->subDays(3))->get();
  // 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));
    }