-
app/Providers/Event.php
Open in GitHub// Usually in the Laravel project there's just EventServiceProvider to register all events // But in this case, they created a separate Event class that extends the default provider use Illuminate\Foundation\Support\Providers\EventServiceProvider as Provider; class Event extends Provider { protected $listen = [ // ... 'App\Events\Document\DocumentViewed' => [ 'App\Listeners\Document\MarkDocumentViewed', ], ]; }
-
app/Events/Document/DocumentViewed.php
Open in GitHub// All that class actually does is taking care of the $document parameter passed to the Event class DocumentViewed extends Event { public $document; public function __construct($document) { $this->document = $document; } }
-
app/Listeners/Document/MarkDocumentViewed.php
Open in GitHub// Then, that parameter is accessible as $event->document inside of this Listener class class MarkDocumentViewed { public function handle(Event $event) { $document = $event->document; if ($document->status != 'sent') { return; } unset($document->paid); $document->status = 'viewed'; $document->save(); // ... the rest of the listener actions } }
-
app/Http/Controllers/Portal/Invoices.php
Open in GitHub// Finally, this is how we call this Event from the Controller, passing $invoice as the document class Invoices extends Controller { public function show(Document $invoice, Request $request) { event(new \App\Events\Document\DocumentViewed($invoice)); return view('portal.invoices.show', compact('invoice', 'payment_methods')); } }