Я пытаюсь обновить уже сохраненную запись, которая включает изображение и путь к изображению в базе данных. Когда я обновляю запись, изображение загружается, и перенаправление работает и показывает сообщение об успехе, однако ни один из столбцов в базе данных не обновляется с новыми данными. Почему это происходит ??
Вот мой метод обновления:
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>