It seems like you're encountering a common issue related to the serialization of Eloquent models when queuing notifications in Laravel. When you dispatch a job to the queue, Laravel serializes the job, including any Eloquent models it contains, to store it in the queue. Upon execution, the job is unserialized and the models are re-retrieved from the database.
The problem you're facing is likely due to the fact that when the WinnerCongratulated notification is serialized, the loaded relationships on the Leaderboard model are also serialized. However, when the job is processed from the queue, the relationships are reloaded from the database without the constraints you initially applied (in this case, the where condition on the my_fighters.user_id).
To resolve this issue, you can explicitly pass only the necessary data to the notification, rather than the entire Eloquent model with its relationships. Here's how you can modify your code:
foreach ($leaderboards as $leaderboard) {
// Retrieve the necessary data from the model and its relationships
$data = [
'leaderboard' => $leaderboard->only(['id', 'user_id', 'league_id', 'event_id', 'points', 'global_champion', 'league_champion']),
'event' => $leaderboard->event->only(['id', 'name', 'date']),
'user' => $leaderboard->user->only(['id', 'name']),
'league' => $leaderboard->league->only(['id', 'name']),
'my_fighters' => $leaderboard->league->myFighters->map->only(['id', 'user_id', 'league_id', 'discipline', 'gender', 'tone']),
];
// Send the notification with the data array
Notification::send(Auth::user(), new WinnerCongratulated($data));
}
And then, in your WinnerCongratulated notification, you would accept the data array and use it directly, rather than relying on the Leaderboard model:
class WinnerCongratulated extends Notification implements ShouldQueue
{
use Queueable;
private $data;
public function __construct($data)
{
$this->data = $data;
}
// Other methods...
}
This way, you're ensuring that only the data you've explicitly passed is used in the notification, and you're avoiding the issue of the entire model being reloaded from the database when the job is processed from the queue.