generated from Flycro/laravel-nuxt
feat: Realtime for Votes + Deadlines
This commit is contained in:
40
app/Events/Deadline/DeadlineCreated.php
Normal file
40
app/Events/Deadline/DeadlineCreated.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events\Deadline;
|
||||
|
||||
use App\Models\Deadline;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class DeadlineCreated implements ShouldBroadcast
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*/
|
||||
public function __construct(public Deadline $deadline)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function broadcastAs(): string
|
||||
{
|
||||
return 'DeadlineCreated';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channels the event should broadcast on.
|
||||
*
|
||||
* @return array<int, \Illuminate\Broadcasting\Channel>
|
||||
*/
|
||||
public function broadcastOn(): array
|
||||
{
|
||||
return [
|
||||
new PrivateChannel('Deadline'),
|
||||
];
|
||||
}
|
||||
}
|
||||
42
app/Events/Vote/VoteCreated.php
Normal file
42
app/Events/Vote/VoteCreated.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events\Vote;
|
||||
|
||||
use App\Models\Vote;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class VoteCreated implements ShouldBroadcast
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
/**
|
||||
* Create a new event instance.
|
||||
*/
|
||||
public function __construct(
|
||||
public Vote $vote
|
||||
)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function broadcastAs(): string
|
||||
{
|
||||
return 'VoteCreated';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channels the event should broadcast on.
|
||||
*
|
||||
* @return array<int, \Illuminate\Broadcasting\Channel>
|
||||
*/
|
||||
public function broadcastOn(): array
|
||||
{
|
||||
return [
|
||||
new PrivateChannel('Vote'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Events\Deadline\DeadlineCreated;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
@@ -16,6 +17,13 @@ class Deadline extends Model
|
||||
'target_chapter',
|
||||
];
|
||||
|
||||
protected static function booted() :void
|
||||
{
|
||||
static::created(static function ($deadline) {
|
||||
event(new DeadlineCreated($deadline));
|
||||
});
|
||||
}
|
||||
|
||||
public function bookRecommendation()
|
||||
{
|
||||
return $this->belongsTo(BookRecommendation::class);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Events\Vote\VoteCreated;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
@@ -14,6 +15,13 @@ class Vote extends Model
|
||||
'book_recommendation_id',
|
||||
];
|
||||
|
||||
protected static function booted() :void
|
||||
{
|
||||
static::created(static function ($vote) {
|
||||
broadcast(new VoteCreated($vote))->toOthers();
|
||||
});
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
|
||||
Reference in New Issue
Block a user