Laravel Event Listener for keeping history
App\Providers\EventServiceProvider.php
protected $listen = [
'App\Events\ResolveHistoryEvent'=>[
'App\Listeners\ResolveHistoryEventListener'
]
];
php artisan event:generate
App\Modules\Tickets\Controllers\TicketsController.php
public function reopenTicket($ticketId){
$ticket = Tickets::find($ticketId);
$ticket->resolve_status = 2;
$ticket->reopen_time = Carbon::now();
$ticket->save();
event(new ResolveHistoryEvent($ticket));
return redirect('tickets/active-list')->with('flash_success','Ticket reopen successfully!');
}
protected $listen = [
'App\Events\ResolveHistoryEvent'=>[
'App\Listeners\ResolveHistoryEventListener'
]
];
php artisan event:generate
App\Modules\Tickets\Controllers\TicketsController.php
public function reopenTicket($ticketId){
$ticket = Tickets::find($ticketId);
$ticket->resolve_status = 2;
$ticket->reopen_time = Carbon::now();
$ticket->save();
event(new ResolveHistoryEvent($ticket));
return redirect('tickets/active-list')->with('flash_success','Ticket reopen successfully!');
}
App\Events\ResolveHistoryEvent.php
public $resolveHistory;
public function __construct($ticket)
{
$this->resolveHistory = $ticket;
}
App\Listeners\ResolveHistoryEventListener.php
public function recordTicketResolveHistory($ticket){
$resolveHistory = new TicketsResolveHistory();
$resolveHistory->ticket_id = $ticket->id;
$resolveHistory->resolve_status = $ticket->resolve_status;
$resolveHistory->save();
}
public function handle(ResolveHistoryEvent $event){
$ticket = $event->resolveHistory;
$this->recordTicketResolveHistory($ticket);
}
Comments
Post a Comment