laravel-learn-bbs/app/Observers/ReplyObserver.php

31 lines
695 B
PHP
Raw Normal View History

2018-01-15 15:24:11 +00:00
<?php
namespace App\Observers;
use App\Models\Reply;
2018-01-27 10:35:56 +00:00
use App\Notifications\TopicReplied;
2018-01-15 15:24:11 +00:00
// creating, created, updating, updated, saving,
// saved, deleting, deleted, restoring, restored
class ReplyObserver
{
public function creating(Reply $reply)
{
2018-01-15 15:59:30 +00:00
$reply->setAttribute('content', clean($reply->getAttribute('content'), 'user_topic_body'));
2018-01-15 15:24:11 +00:00
}
public function updating(Reply $reply)
{
//
}
2018-01-15 15:59:30 +00:00
public function created(Reply $reply)
{
2018-01-27 10:35:56 +00:00
$topic = $reply->topic;
2018-01-15 15:59:30 +00:00
$reply->topic->increment('reply_count', 1);
2018-01-27 10:35:56 +00:00
if (!$reply->user->isAuthorOf($topic)) {
$topic->user->notify(new TopicReplied($reply));
}
2018-01-15 15:59:30 +00:00
}
2018-01-15 15:24:11 +00:00
}