更新我的Post模型时,我运行:
$post->title = request('title'); $post->body = request('body'); $post->save();
这不会更新我的帖子.但它应该根据the Laravel docs on updating Eloquent models.为什么我的模型没有更新?
>我没有错误.
>该帖子未在db中更新.
>除了没有在数据库中更新,没有别的东西似乎奇怪.没有错误.行为正常.
> running this test to see if save
succeeded的结果是真的.
> This Laravel thread没有帮助
发布模型:
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与之前的帖子相同而没有更新的数据.
由于Laravel 5.5 laravel已经改变了一些验证机制,我想你需要尝试这种方式.
public function store(Request $request,$id) { $post = Post::findOrFail($id); $validatedData = []; // Request validation if ($post->type == 1) { // Post type has title $validatedData = $request->validate([ 'title' => 'required|min:15',]); } else { $validatedData = $request->validate([ 'body' => 'required|min:19',]); } $post->update($validatedData); return redirect('/'); }