【发布时间】:2018-04-05 19:14:39
【问题描述】:
更新我的Post 模型时,我运行:
$post->title = request('title');
$post->body = request('body');
$post->save();
这不会更新我的帖子。但它应该根据the Laravel docs on updating Eloquent models。 为什么我的模型没有更新?
- 我没有收到任何错误。
- 帖子未在数据库中更新。
- 除了没有在数据库中更新之外,没有其他任何奇怪的地方。没有错误。行为正常。
-
running this test to see if
savesucceeded 的结果是true。 - This Laravel thread 没有帮助
Post模特:
class Post extends Model
{
protected $fillable = [
'type',
'title',
'body',
'user_id',
];
....
}
Post控制器:
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 与之前的帖子相同没有更新数据。
【问题讨论】:
-
您确定在 POST 模型下有“body”字段吗?
-
@MoeenBasra 是的,对不起!我已经清理了其他代码以使其更具可读性和相关性(删除了不相关的部分,例如我已经验证过的代码清理等),但我忘记了更改该变量。
-
什么返回
dd($post->save())? -
@IvankaTodorova
dd($post->save())返回true。我在问题中添加了这个。 -
注入到 store 函数或请求助手的 $request 返回相同的东西。由于请求与带有容器的单例模式绑定。
标签: php laravel laravel-5 eloquent laravel-5.5