laravel-learn-bbs/app/Observers/ReplyObserver.php
2018-01-27 18:35:56 +08:00

31 lines
695 B
PHP

<?php
namespace App\Observers;
use App\Models\Reply;
use App\Notifications\TopicReplied;
// creating, created, updating, updated, saving,
// saved, deleting, deleted, restoring, restored
class ReplyObserver
{
public function creating(Reply $reply)
{
$reply->setAttribute('content', clean($reply->getAttribute('content'), 'user_topic_body'));
}
public function updating(Reply $reply)
{
//
}
public function created(Reply $reply)
{
$topic = $reply->topic;
$reply->topic->increment('reply_count', 1);
if (!$reply->user->isAuthorOf($topic)) {
$topic->user->notify(new TopicReplied($reply));
}
}
}