我正在尝试更新已保存的记录,其中包括数据库中的图像和图像路径。当我更新记录时,图像被上传,并且重定向工作并显示成功消息,但是,数据库中的所有列都没有使用新数据进行更新。为什么会这样?
这是我的更新方法:
public function update(Request $request, $id)
{
if ($request['post_image'] != '') {
$request->validate([
'title' => 'required',
'description' => 'required',
'slug' => 'required',
'message' => 'required',
'user' => 'required',
'post_image' => 'image|mimes:jpeg,png,jpg,gif|max:2048'
]);
$image_path = Storage::disk('public')->putFile('uploads/images', $request->file('post_image'));
$request['image_path'] = $image_path;
}else{
$request->validate([
'title' => 'required',
'description' => 'required',
'slug' => 'required',
'message' => 'required',
'user' => 'required'
]);
}
$update = [
'title' =>$request->title,
'description' =>$request->description,
'slug' =>$request->slug,
'message' =>$request->message,
'user' =>$request->user,
'post_image' =>$request->image_path];
Post::where('id', $id)->update($update);
return Redirect::to('admin')->with('success','Great! Post updated successfully');
}
这是视图:
<form action="{{ route('blog.update', $post_info->slug) }}" method="POST" name="edit_post" role="form" enctype="multipart/form-data">
{{ csrf_field() }}
@method('PATCH')
<h1>Edit Post</h1>
<div role="separator" class="dropdown-divider"></div>
<div class="form-row">
<div class="form-group col-12 col-md-6">
<label for="title">Post Title</label>
<input type="text" autocomplete="off" class="form-control" id="title" name="title" placeholder="Your post title" value="{{$post_info->title}}" required>
<span class="text-danger">{{ $errors->first('title') }}</span>
</div>
<div class="form-group col-12 col-md-6">
<label for="slug">Slug</label>
<input type="text" autocomplete="off" class="form-control" id="slug" name="slug" placeholder="Write post slug" value="{{$post_info->slug}}" required>
<span class="text-danger">{{ $errors->first('slug') }}</span>
</div>
</div>
<div class="form-row">
<div class="form-group col-12 col-md-12">
<label for="description">Post Description</label>
<textarea class="form-control" id="description" name="description" placeholder="Enter a small description for your post" required>{{$post_info->description}}</textarea>
<span class="text-danger">{{ $errors->first('description') }}</span>
</div>
</div>
<div class="badge badge-warning badge-pill">Message</div>
<div role="separator" class="dropdown-divider"></div>
<div class="form-row">
<div class="form-group col-md-12">
<textarea class="form-control" col="4" id="message" name="message">{{$post_info->message}}</textarea>
<span class="text-danger">{{ $errors->first('message') }}</span>
</div>
@if($post_info->post_image)
<img src="{{ asset('storage/' . $post_info->post_image) }}" class="img-thumbnail img-fluid blog-img">
@endif
<input id="post_image" type="file" class="form-control" name="post_image">
</div>
<input type="hidden" value="{{ Auth::user()->name }}" name="user">
<button type="submit" class="btn btn-warning btn-block">Edit Post</button>
</form>
这行是错误的:
Post::where('id', $id)->update($update);
因为where
返回一个Eloquent Builder对象,而不是Model实例,该实例代表您想从数据库中获取的记录,所以您可以执行以下操作:
Post::where('id', $id)->firstOrFail()->update($update); /* or Post::where('id', $id)->first()->update($update); if you are sure that the record is found */
还是if id
是主键:
Post::findOrFail($id)->update($update); /* or Post::find($id)->update($update); if you are sure that the record is found */