如果项目(多个项目)在几天后到期,如何通知管理员-Laravel [英] How to notify the admin if the items(Multiple items) are going to expire in days later-Laravel

查看:122
本文介绍了如果项目(多个项目)在几天后到期,如何通知管理员-Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目表中有很多项目,并且项目表中有过期日期列。我只想每天在每个项目在一个日期之前过期之前获得一个推送通知(过期日期彼此不同,可以有多个过期日期来使多个项目失效)。我是Laravel的新手。请帮助我。

There are many items in the Items table and there is an expired date column in the items table. I just want to get a push notification every day before each items going expire one date before (Expire dates are different to each other and can have one expire date to multuple items). im new to laravel. Please help me.

推荐答案

这可以通过 Laravel任务计划



This can be simply done with Laravel task scheduling


  1. 您需要创建一个自定义工匠命令喜欢

php artisan make:command SendItemExpiryEmailsToAdmin









  1. App下Console\Commands 您会发现应该创建一个类 SendItemExpiryEmailsToAdmin

  1. Under App\Console\Commands You will find a class SendItemExpiryEmailsToAdmin should be created.

i)首先,您需要定义命令的签名,该签名将用于从命令行调用。

i) First you need to define the signature of the command which will be used to call from the command line.

protected $signature = 'email:send-item-expiry-email-to-admin';

ii)在 handle()的内部同一堂课,写下你的逻辑。下面给出了示例逻辑。您需要创建一个可邮寄类来发送邮件。

ii) Inside the handle() of the same class, write your logic. A sample logic is given below. You will need to create a Mailable class to send the mail.

public function handle() {
    // To fetch all the items ids which are going to expired today.
    $itemsIds = Items::whereBetween('expired',
            [Carbon::now()->setTime(0,0)->format('Y-m-d H:i:s'),
            Carbon::now()->setTime(23,59,59)->format('Y-m-d H:i:s')])
         ->pluck('id')
         ->toArray();
    $itemsIds = implode(" ", $itemsIds);
    Mail::queue(new ItemsExpiryEmail($itemsIds));
    // If you are not using queue, just replace `queue` with `send` in above line
}

上将`queue`替换为`send`。 li>








  1. 可邮寄以发送邮件。

i)运行以下命令以创建可邮寄邮件

i) Run the following command to create a mailable

php artisan make:mail ItemsExpiryEmail

ii)在邮件中,编写您的代码。下面给出了示例代码,您可以在邮件视图中使用 $ this-> itemIds ,因为它是公共变量。

ii) In the mailable, write your code. A sample code is given below, you can use $this->itemIds in the mail view as it is a public variable.

class ItemsExpiryEmail extends Mailable
{
    use Queueable, SerializesModels; // don't forget to import these.
    public $itemIds;

    public function __construct($itemIds)
    {
        $this->itemIds = $itemIds;
    }

    public function build()
    {
        return $this->view('emails.orders.shipped');
        return $this->to('test@example.com', 'Admin')
            ->subject('Subject of the mail')
            ->view('emails.adminNotification'); // adminNotification will be the view to be sent out as email body
    }
}









  1. 该命令需要每天使用玉米工作

  1. This command needs to be executed daily using the Cornjob.

尝试一下,我相信这会有所帮助。让我知道是否有任何疑问。

Try this, I'm sure this will help. Let me know if you have any questions.

这篇关于如果项目(多个项目)在几天后到期,如何通知管理员-Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆