root 5 years ago
parent
commit
c749e31182

+ 38 - 0
app/Events/ServerLogSaveEvent.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace App\Events;
+
+use App\Models\ServerLog;
+use Illuminate\Broadcasting\Channel;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Broadcasting\PresenceChannel;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
+
+class ServerLogSaveEvent
+{
+    use Dispatchable, InteractsWithSockets, SerializesModels;
+
+    public $serverLog;
+    /**
+     * Create a new event instance.
+     *
+     * @return void
+     */
+    public function __construct(ServerLog $serverLog) {
+        $this->serverLog = $serverLog;
+    }
+
+    /**
+     * Get the channels the event should broadcast on.
+     *
+     * @return \Illuminate\Broadcasting\Channel|array
+     */
+    public function broadcastOn()
+    {
+        return new PrivateChannel('channel-name');
+    }
+}
+

+ 30 - 0
app/Listeners/ServerLogSaveListener.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Listeners;
+
+use App\Events\ServerLogSaveEvent;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Contracts\Queue\ShouldQueue;
+
+class ServerLogSaveListener
+{
+    /**
+     * Create the event listener.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        //
+    }
+
+    /**
+     * Handle the event.
+     *
+     * @param  ServerLogSaveEvent  $event
+     * @return void
+     */
+    public function handle(ServerLogSaveEvent $event) {
+        info($event->serverLog);
+    }
+}

+ 4 - 0
app/Models/ServerLog.php

@@ -2,10 +2,14 @@
 
 namespace App\Models;
 
+use App\Events\ServerLogSaveEvent;
 use Illuminate\Database\Eloquent\Model;
 
 class ServerLog extends Model
 {
     protected $table = 'v2_server_log';
     protected $dateFormat = 'U';
+    protected $dispatchesEvents = [
+        'saving' => ServerLogSaveEvent::class,
+    ];
 }

+ 2 - 4
app/Providers/EventServiceProvider.php

@@ -2,8 +2,6 @@
 
 namespace App\Providers;
 
-use Illuminate\Auth\Events\Registered;
-use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
 use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
 use Illuminate\Support\Facades\Event;
 
@@ -15,8 +13,8 @@ class EventServiceProvider extends ServiceProvider
      * @var array
      */
     protected $listen = [
-        Registered::class => [
-            SendEmailVerificationNotification::class,
+        'App\Events\ServerLogSaveEvent' => [
+            'App\Listeners\ServerLogSaveListener',
         ],
     ];