Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

php - How to use Laravel queue for schedule notification

I want to send a summary of the year's activities for active users. The summary will simply display stuff like no of items purchased, total amount etc. Due to the fact that I will be sending the notification to over 1k users at a scheduled time, I have decided to use queues. Strangely the queues keep failing but when I do not queue, the notifications are delivered successfully ( tested using Mailhog from Homestead setup ).

Below is the loop in my schedule that fires the notification for each active user

foreach ($users as $user) {

    if (...condition) {

        $totalSpent = Purchase::where(...condition)->sum('amount');

        $maxAmountSpent = Purchase::where(...condition)->max('total_amount');

        $noOfPurchase = $user->purchase->where(...condition)->count();
        
        $user->notify(new Summary($user, $totalSpent, $maxAmountSpent, $noOfPurchase));
    }
}

The summary notification is like this

class Summary extends Notification implements ShouldQueue
{
    use Queueable;

    public $user; 
    public $totalSpent; 
    public $maxAmountSpent; 
    public $noOfPurchase;

    public function __construct($user, $totalSpent, $maxAmountSpent, $noOfPurchase)
    {
        $this->user = $user; 
        $this->totalSpent = $totalSpent; 
        $this->maxAmountSpent = $maxAmountSpent; 
        $this->noOfPurchase = $noOfPurchase;

        $this->delay(10);
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->from(...)
            ->subject(...)
            ->view('mails.scorecard', [
                'user' => $this->user,
                'totalSpent' => $this->totalSpent, 
                'maxAmountSpent' => $this->maxAmountSpent,
                'noOfPurchase' => $this->noOfPurchase,
            ]);
    }

    ...
}

And the blade template consumes the variables

<table class="transaction-details">
    <tr class="txn-row">
        <td width="50%" class="half-column">
            <p class="cell-title">
                Total Amount
            </p>
            <p class="cell-value">
                <span class="currency">NGN</span>
                <span class="txn-amount">{{number_format($totalSpent,2)}}</span>
            </p>
        </td>
        
        <td width="50%" class="half-column">
            <p class="cell-title">
                Maximum Amount
            </p>
            <p class="cell-value">
                <span class="txn-amount">{{$maxAmountSpent}}</span>
            </p>
        </td>
    </tr>
</table>

Again the issue is that without queuing the notification gets delivered successfully but fails when using queues then throws the exception error below

Undefined variable: totalSpent (View: /project-path/resources/views/mails/summary.blade.php)

How can I fix this error so I can use queues or is there a better way to achieve sending the notification to my 1k users?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...