当我更新我的Post
模型时,我运行以下代码:
$post->title = request('title');
$post->body = request('body');
$post->save();
根据Laravel更新Eloquent模型的文档,这不会更新我的帖子。但是应该更新。 为什么我的模型没有被更新?
- 我没有收到任何错误信息。
- 文章在数据库中没有更新。
- 除了在数据库中没有更新之外,没有其他异常情况。 没有错误。 行为正常。
- 运行此测试以查看
save
是否成功的结果是true
。 - 此Laravel主题没有提供帮助
Post
模型:
class Post extends Model
{
protected $fillable = [
'type',
'title',
'body',
'user_id',
];
....
}
帖子
控制器:
public function store($id)
{
$post = Post::findOrFail($id);
// Request validation
if ($post->type == 1) {
// Post type has title
$this->validate(request(), [
'title' => 'required|min:15',
'body' => 'required|min:19',
]);
$post->title = request('title');
$post->body = request('body');
} else {
$this->validate(request(), [
'body' => 'required|min:19',
]);
$post->body = request('body');
}
$post->save();
return redirect('/');
}
奖励信息
运行dd($post->save())
返回true
。
运行
$post->save();
$fetchedPost = Post::find($post->id);
dd($fetchedPost);
这告诉我$fetchedPost
是之前的同一篇文章,但没有更新的数据。
false
...... 我按照这个 Stack Overflow 的问题来确保我做得正确。 - Jacob