添加话题回复时出现Call to ... App\\Models\\Topic::updateReplyCount()
测试增加话题回复时,报错如下图:
应该是触发了教程L02的更新话题回复数的观察器,不知道为何没有调用App\Models\Reply里的updateReplyCount,而是去报这个错
Call to undefined method App\Models\Topic::updateReplyCount():
ReplyObserver.php
<?php
namespace App\Observers;
use App\Models\Reply;
use App\Notifications\TopicReplied;
class ReplyObserver
{
//统计回复数
public function created(Reply $reply) {
// 命令行运行迁移时不做这些操作!
if ( ! app()->runningInConsole()) {
$reply->topic->updateReplyCount();
// 通知话题作者有新的评论
$reply->topic->user->notify(new TopicReplied($reply));
}
}
}
Models 里的Reply.php
<?php
namespace App\Models;
class Reply extends Model
{
protected $fillable = ['content'];
public function topic()
{
return $this->belongsTo(Topic::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
public function updateReplyCount()
{
$this->reply_count = $this->replies->count();
$this->save();
}
}
推荐文章: