Kaynağa Gözat

Use database for events

php artisan queue:work
axkuhta 1 yıl önce
ebeveyn
işleme
3035296eb0

+ 6 - 14
app/Events/CommentAdded.php

@@ -10,27 +10,19 @@ use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
 use Illuminate\Foundation\Events\Dispatchable;
 use Illuminate\Queue\SerializesModels;
 
+use App\Models\Comment;
+
 class CommentAdded
 {
     use Dispatchable, InteractsWithSockets, SerializesModels;
 
-    /**
-     * Create a new event instance.
-     */
-    public function __construct()
-    {
-        //
-    }
+    public Comment $comment;
 
     /**
-     * Get the channels the event should broadcast on.
-     *
-     * @return array<int, \Illuminate\Broadcasting\Channel>
+     * Create a new event instance.
      */
-    public function broadcastOn(): array
+    public function __construct(Comment $comment)
     {
-        return [
-            new PrivateChannel('channel-name'),
-        ];
+    	$this->comment = $comment;
     }
 }

+ 5 - 3
app/Listeners/PremoderateComment.php

@@ -6,7 +6,9 @@ use App\Events\CommentAdded;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Queue\InteractsWithQueue;
 
-class PremoderateComment
+use App\Models\Comment;
+
+class PremoderateComment implements ShouldQueue
 {
     /**
      * Create the event listener.
@@ -19,8 +21,8 @@ class PremoderateComment
     /**
      * Handle the event.
      */
-    public function handle(CommentAdded $event): void
+	public function handle(CommentAdded $event): void
     {
-        //
+        dump($event);
     }
 }

+ 5 - 0
app/Models/Comment.php

@@ -4,6 +4,7 @@ namespace App\Models;
 
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\SoftDeletes;
+use App\Events\CommentAdded;
 
 class Comment extends Model
 {
@@ -21,6 +22,10 @@ class Comment extends Model
 		"status"
 	];
 
+	protected $dispatchesEvents = [
+		"created" => CommentAdded::class
+	];
+
 	function commentable() {
 		return $this->morphTo();
 	}

+ 32 - 0
database/migrations/2023_12_03_195641_create_jobs_table.php

@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::create('jobs', function (Blueprint $table) {
+            $table->bigIncrements('id');
+            $table->string('queue')->index();
+            $table->longText('payload');
+            $table->unsignedTinyInteger('attempts');
+            $table->unsignedInteger('reserved_at')->nullable();
+            $table->unsignedInteger('available_at');
+            $table->unsignedInteger('created_at');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::dropIfExists('jobs');
+    }
+};