Level 70
@webfuelcode have you tried this way?
public function update(Request $request, Post $post)
{
$validated = $this->validate($request, [
'title' => 'required',
'img1' => 'sometimes|image',
'category_id' => 'required',
'description' => 'required'
]);
// Post image upload
if($request->hasFile('img1')){
$filename = time() . '.' . $request->img1->getClientOriginalExtension();
// Check if you have any attached image with the post.
if($post->img1 !== null){
// You may need to adjust image path, if required.
$image_path = "/images/$post->img1";
if(File::exists($image_path)) {
File::delete($image_path);
}
}
$request->img1->storeAs('post_img', $filename, 'my_files');
$validated['img1'] = $filename;
}
$post->update($validated);
return redirect()->route('post.show', $post->slug)->withMessage('Updated successfully!');
}