Answers for "laravel delete data with iamge"

PHP
2

when image update laravel delete remove image

public function update(Post $post){
    $this->validateRequest();
    $data = [
        'title' => request()->title,
        'content' => request()->content
    ];
    if (request()->hasFile('image') && request('image') != '') {
        $imagePath = public_path('storage/'.$post->image);
        if(File::exists($imagePath)){
            unlink($imagePath);
        }
        $image = request()->file('image')->store('uploads', 'public');
        $data['image'] = $image;
        $post->update($data);
    }
    $post->update($data);
}
Posted by: Guest on April-21-2021
1

how to delete image from floder in laravel

//suppose you have an image inside public/images folder 
$image_path = "/images/filename.ext";  // Value is not URL but directory file path
if(File::exists($image_path)) {
    File::delete($image_path);
}
//don't forget to add "use Illuminate\Support\Facades\File;"
Posted by: Guest on April-05-2022

Browse Popular Code Answers by Language