How to notify the admin if the items(Multiple items) are going to expire in days later-Laravel

为君一笑 提交于 2020-01-05 04:07:11

问题


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.


回答1:


This can be simply done with Laravel task scheduling

  1. You need to create a custom artisan command like

    php artisan make:command SendItemExpiryEmailsToAdmin
    
  2. Under App\Console\Commands You will find a class SendItemExpiryEmailsToAdmin should be created.

    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) 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
    }
    

3 This command needs to be executed daily using the cronjob.

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




回答2:


You can create Task scheduler in Laravel. you can get more info from it's doc here: https://laravel.com/docs/5.8/scheduling

1) you get create login in the scheduling code like. get the all the items which is expiring tomorrow, And send the detail of items to the admin email.

if you have some knowledge in user define artisan command you can also implement scheduling-artisan-commands https://laravel.com/docs/5.8/scheduling#scheduling-artisan-commands.



来源:https://stackoverflow.com/questions/59550293/how-to-notify-the-admin-if-the-itemsmultiple-items-are-going-to-expire-in-days

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!