为什么值一直是 null,再次简化了流程测试,还是 null
我想在每次留言和删除留言的时候统计留言的数量,然后更新到对应的字段里,之前发了一个问题,可能是流程没有表达清楚,回答的人很少,我现在简化一下问题
TestPsy.php
public function testreplies()
{
return $this->hasMany(TestReply::class);
}
TestReply.php
public function testpsy()
{
return $this->belongsTo(TestPsy::class);
}
controller
public function store(TestReplyRequest $request)
{
$testreply = TestReply::create($request->all());
$testreply->testpsy->increment('reply_count', 1);
session()->flash('success', '发布评论成功!');
return redirect()->route('testreplies.show', $request->test_psy_id);
}
$testreply->testpsy
这个就是null,为什么?$testreply
是有值的
controller换了个写法可以了,但是到底是哪里的问题呢:
修改为:
因为你一直访问一个为null的数据
dump一下$testreplay看看结果是什么吧