Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

isaacdarcilla's avatar

Queue job in notification not updating

Laravel Version: 8.12 PHP Version: 7.4 Database MySQL Description:

I have this piece of code that loops student, updates the data in the array, then send the notification using queue. The problem is that the data in notification is not updating dynamically, it always sends the first data of student. How can I resolve this?

Already tried, restarting the queue worker, clearing cache, opcache.

    foreach ($students as $student) {
        $student_data = [
            "%%school_year%%" => SchoolYear::active('enrollment'),
            "%%school_aide_id%%" => $student->school_aide_student_number,
            "%%student_id%%" => $student->student_number,
            "%%student_first_name%%" => strtoupper($student->first_name),
            "%%student_middle_name%%" => strtoupper($student->middle_name),
            "%%student_last_name%%" => strtoupper($student->last_name),
            "%%student_suffix_name%%" => strtoupper($student->suffix_name),
            "%%grade_level%%" => strtoupper($student->grade_level->name ?? "-"),
            "%%section%%" => strtoupper($student->section->name ?? "-"),
            "%%strand%%" => strtoupper($student->strand->name ?? "-"),
            "%%application_status%%" => ucwords($student->application_status),
            "%%application_date%%" => Carbon::parse($student->created_at)->toFormattedDateString(),
            "%%attestation_date%%" => Carbon::parse($student->attested_at)->toFormattedDateString(),
            "%%school_name%%" => Tenant::current()->name,
            "%%school_email_address%%" => Tenant::current()->school_official_email_address ?? "-",
            "%%school_telephone_number%%" => Tenant::current()->school_official_mobile_number ?? "-",
            "%%school_mobile_number%%" => Tenant::current()->school_official_telephone_number ?? "-",
            "%%school_contact_number%%" => Tenant::current()->liason_contact ?? "-",
            "%%payment_schedule%%" => $student->tuitions->isNotEmpty() ? $student->tuitions[0]->name : 'No selected payment schedule',
            "%%payment_amount%%" => $student->transactions->isNotEmpty() ? number_format($student->transactions[0]->amount, 2) : '-',
            "%%payment_or_number%%" => $student->transactions->isNotEmpty() ? '#' . $student->transactions[0]->official_receipt_number : '-',
            "%%payment_or_date%%" => $student->transactions->isNotEmpty() ? Carbon::parse($student->transactions[0]->payment_date)->toFormattedDateString() : '-',
            "%%payment_date%%" => $student->transactions->isNotEmpty() ? Carbon::parse($student->transactions[0]->created_at)->toFormattedDateString() : '-',
            "%%payment_method%%" => $student->transactions->isNotEmpty() ? ucwords(str_replace('_', ' ', $student->transactions[0]->payment_method)) : '-',
            "%%payment_purpose%%" => $student->transactions->isNotEmpty() ? $student->transactions[0]->payment_purpose : '-',
            "%%total_amount_paid%%" => $student->transactions->isNotEmpty() ? number_format($student->transactions->sum('amount')) : '-',
            "%%total_amount_balance%%" => $student->transactions->isNotEmpty() && $student->tuitions->isNotEmpty() ? number_format($student->tuitions[0]->full_tuition_amount - $student->transactions->sum('amount'), 2) : '-',
        ];

        // Mail::to($student->user->email)
        //    ->send(new BroadcastMail($this->title, $this->content));

        foreach ($this->dictionary as $key => $value) {
            if (array_key_exists($value, $student_data)) {
                $this->content = str_replace($value, $student_data[$value], $this->content);
                $this->title = str_replace($value, $student_data[$value], $this->title);
            }
        }

        $student->user->notify(new BroadcastNotification($this->title, $this->content));
}

BroadcastNotification .php

class BroadcastNotification extends Notification implements ShouldQueue { use Queueable;

public $title, $content;

public function __construct($title, $content)
{
    $this->title = $title;
    $this->content = $content;
}

public function via($notifiable)
{
    return ['mail'];
}

public function toMail($notifiable)
{
    return (new MailMessage)
        ->subject($this->title)
        ->view('mail.broadcast-mail', [
            'content' => $this->content,
        ]);
}

public function toArray($notifiable)
{
    return [
        //
    ];
}

}

$student_data when logged inside foreach()

[2022-03-28 11:10:02] local.INFO: array ( '%%school_year%%' => '2022-2023', '%%school_aide_id%%' => 'SA-2022015259', '%%student_id%%' => '2012-79797', '%%student_first_name%%' => 'KRIS', '%%student_middle_name%%' => '', '%%student_last_name%%' => 'DOE', '%%student_suffix_name%%' => '', '%%grade_level%%' => 'JUNIOR NURSERY', '%%section%%' => '-', '%%strand%%' => '-', '%%application_status%%' => 'Pending', '%%application_date%%' => 'Mar 17, 2022', '%%attestation_date%%' => 'Mar 28, 2022', '%%school_name%%' => 'St. Paul University Quezon City ', '%%school_email_address%%' => '[email protected]', '%%school_telephone_number%%' => '09509342323', '%%school_mobile_number%%' => '094214152155', '%%school_contact_number%%' => '09509342325', '%%payment_schedule%%' => 'No selected payment schedule', '%%payment_amount%%' => '-', '%%payment_or_number%%' => '-', '%%payment_or_date%%' => '-', '%%payment_date%%' => '-', '%%payment_method%%' => '-', '%%payment_purpose%%' => '-', '%%total_amount_paid%%' => '-', '%%total_amount_balance%%' => '-', ) [2022-03-28 11:10:02] local.INFO: array ( '%%school_year%%' => '2022-2023', '%%school_aide_id%%' => 'SA-2022020179', '%%student_id%%' => '22-0200058', '%%student_first_name%%' => 'ISAAC', '%%student_middle_name%%' => '', '%%student_last_name%%' => 'ARCILLA', '%%student_suffix_name%%' => '', '%%grade_level%%' => 'JUNIOR NURSERY', '%%section%%' => '-', '%%strand%%' => '-', '%%application_status%%' => 'Approved', '%%application_date%%' => 'Mar 23, 2022', '%%attestation_date%%' => 'Mar 24, 2022', '%%school_name%%' => 'St. Paul University Quezon City ', '%%school_email_address%%' => '[email protected]', '%%school_telephone_number%%' => '09509342323', '%%school_mobile_number%%' => '094214152155', '%%school_contact_number%%' => '09509342325', '%%payment_schedule%%' => 'No selected payment schedule', '%%payment_amount%%' => '-', '%%payment_or_number%%' => '-', '%%payment_or_date%%' => '-', '%%payment_date%%' => '-', '%%payment_method%%' => '-', '%%payment_purpose%%' => '-', '%%total_amount_paid%%' => '-', '%%total_amount_balance%%' => '-', )

$student_data when logged inside BroadcastNotification.php

[2022-03-28 11:12:02] local.INFO: SA-2022015259 [2022-03-28 11:12:02] local.INFO: SA-2022015259

The data is not updated in BroadcastNotification class

0 likes
6 replies
bobbybouwmann's avatar

The problem is here

foreach ($this->dictionary as $key => $value) {
	if (array_key_exists($value, $student_data)) {
		$this->content = str_replace($value, $student_data[$value], $this->content);
		$this->title = str_replace($value, $student_data[$value], $this->title);
	}
}

You always loop over the same $this-dictionary value. I don't see how this works as expected.

Also, you are always referring to $this->content and $this->title. Why not create a temporary variable. Your code is now really unreadable since you always refer to those existing properties.

bobbybouwmann's avatar
Level 88

@isaacdarcilla I would personally put the data of $this->content in a variable in the method before the foreach

$content = $this->content;

// Or
$content = 'my content goes here'; // I find this more readable
1 like
Snapey's avatar

the answer is simple, take a copy of $this->content before overwriting it

on the first pass it contains your placeholders

on the second pass it contains student data and no placeholders - so nothing changes

but, why aren't you using a blade template for this?

1 like

Please or to participate in this conversation.