问题
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
You need to create a custom artisan command like
php artisan make:command SendItemExpiryEmailsToAdmin
Under
App\Console\Commands
You will find a classSendItemExpiryEmailsToAdmin
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