create broadcast notification when the user role is updated

This commit is contained in:
2026-03-19 17:11:34 +01:00
parent bda5aa4d50
commit 16ae03e6e6
2 changed files with 52 additions and 1 deletions
+45
View File
@@ -0,0 +1,45 @@
<?php
namespace App\Events;
use App\Models\Event;
use App\Models\Task;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class VolunteerRoleUpdated implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $role;
protected $userId;
/**
* Create a new event instance.
*/
public function __construct($userId, $role)
{
$this->userId = $userId;
$this->role = $role;
}
/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('user.' . $this->userId),
];
}
public function broadcastAs()
{
return 'volunteer.role.updated';
}
}
+7 -1
View File
@@ -3,6 +3,7 @@
namespace App\Http\Controllers;
use App\Events\UserCreated;
use App\Events\VolunteerRoleUpdated;
use App\Mail\CreateUserByAdminMail;
use App\Mail\DeactivateAccountMail;
use App\Mail\DeleteAccountMail;
@@ -255,10 +256,15 @@ class UserController extends Controller
$role = \App\Services\GetRole::getRole($request['role']);
$notification = Notification::create([
'content' => "Votre rôle à changé pour {$role} !",
'content' => "Votre rôle à changé pour : {$role} !",
]);
$notification->users()->attach($user->id);
broadcast(new VolunteerRoleUpdated(
$user->id,
$role
));
$data = [
'name' => $user->name,
'lastname' => $user->lastname,