在 Laravel 中发送电子邮件后,在 table 中设置 "status = sent"

Set "status = sent" in table after email sent in Laravel

我的应用程序通过电子邮件向用户发送提要。为此,我创建了一个命令名称 SendFeedEmails.php 来发送电子邮件。

以上命令将获取今天的所有提要并将 user_id 存储在数组中并执行名为 sendEmailToUser 的私有函数。

通过该功能,所有数据都将 FeedEmailDigest 可邮寄 class.

但我想将状态设置为 sent in table named feed_statuses after发送给用户的电子邮件。

  1. SendFeedEmails.php(命令)
<?php

namespace App\Console\Commands;

use App\User;
use App\FeedStatus;
use App\Mail\FeedEmailDigest;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;

class SendFeedEmails extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'feed:emails';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Send email notification to users about feeds.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // Get all feeds for today
        $feeds = FeedStatus::query()
            ->with(['feeds'])
            ->where('msg_date', now()->format('Y-m-d'))
            ->where('status', 'pending')
            ->orderBy('user_id')
            ->get();

        // Group by user
        $data = [];
        foreach ($feeds as $feed) {
            $data[$feed->user_id][] = $feed->toArray();
        }

        //dd($data);

        foreach ($data as $userId => $feeds) {
            $this->sendEmailToUser($userId, $feeds);

        }
        
        // Send email
        return 0;
    }

    private function sendEmailToUser($userId, $feeds)
    {
        $user = User::find($userId);
        Mail::to($user)->send(new FeedEmailDigest($feeds));
    }
}
  1. FeedEmailDigest.php(邮件)
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class FeedEmailDigest extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    private $feeds;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($feeds)
    {
        $this->feeds = $feeds;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.feed-digest')
            ->with('feeds', $this->feeds);
    }
}
  1. feed_statuses(table)

尝试以下方法

将sendmail函数修改为..

    private function sendEmailToUser($userId, $feeds)
    {
        $user = User::find($userId);
        //Since you expect to have more than one entry for same user eg. feed 1 user 1, feed 2 user 1,   a loop will be in order
        foreach($feeds as $feed){
          $affected = DB::table('feed_statuses')->where(['user_id'=> $userId , 'feed_id' => $feed->id])->update(['status' => 'sent']);
        }
        Mail::to($user)->send(new FeedEmailDigest($feeds));

    }