add reverb container, unread attribute in notification pivot table and private channel and event for new register
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Broadcasting\Channel;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class UserCreated implements ShouldBroadcastNow
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
|
||||
public $user;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*/
|
||||
public function __construct(User $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channels the event should broadcast on.
|
||||
*
|
||||
* @return array<int, \Illuminate\Broadcasting\Channel>
|
||||
*/
|
||||
public function broadcastOn(): array
|
||||
{
|
||||
return [
|
||||
new privateChannel('admins-private'),
|
||||
];
|
||||
}
|
||||
|
||||
public function broadcastAs()
|
||||
{
|
||||
return 'user.created';
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\UserCreated;
|
||||
use App\Models\Notification;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
@@ -51,6 +52,10 @@ class UserController extends Controller
|
||||
$admins->pluck('id')->toArray()
|
||||
);
|
||||
|
||||
Log::info('Broadcasting UserCreated event', ['user_id' => $user->id]);
|
||||
broadcast(new UserCreated($user));
|
||||
Log::info('Event broadcasted');
|
||||
|
||||
return response()->json(['message' => 'User created successfully']);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ class Notification extends Model
|
||||
'notification_user',
|
||||
'notification_id',
|
||||
'user_id'
|
||||
)->withTimestamps();
|
||||
)
|
||||
->withPivot('unread')
|
||||
->withTimestamps();
|
||||
}
|
||||
}
|
||||
|
||||
+4
-2
@@ -61,7 +61,9 @@ class User extends Authenticatable
|
||||
Notification::class,
|
||||
'notification_user',
|
||||
'user_id',
|
||||
'notification_id'
|
||||
)->withTimestamps();
|
||||
'notification_id',
|
||||
)
|
||||
->withPivot('unread')
|
||||
->withTimestamps();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,5 +24,6 @@ class AppServiceProvider extends ServiceProvider
|
||||
Route::middleware('api')->prefix('api')->group(base_path('routes/auth.php'));
|
||||
Route::middleware('api')->prefix('api')->group(base_path('routes/events.php'));
|
||||
Route::middleware('api')->prefix('api')->group(base_path('routes/notifications.php'));
|
||||
Route::middleware('api')->prefix('api')->group(base_path('routes/channels.php'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Broadcast;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class BroadcastServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function boot(): void
|
||||
{
|
||||
Broadcast::routes(['middleware' => ['web', 'auth:sanctum']]);
|
||||
|
||||
require base_path('routes/channels.php');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user