Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

akaunting/akaunting

9348 stars
4 code files
View akaunting/akaunting on GitHub

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'));
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.