Neeraj1005's avatar

Remove images from storage folder in Laravel

In my laravel project If I delete the image, it deletes the image from database but I want to delete image also from my storage folder. In my project I have stored image in public/uploads/media folder. can anyone tell me how to do this? This is my store and delete function

public function store(Request $request)
    {
        $request->validate([
            'name'=>'required',
            'image'=>'required',
        ]);
        $image = $request->file('image');
        $extension = $image->getClientOriginalExtension();
        $originalname = $image->getClientOriginalName();
        $path = $image->move('uploads/media/', $originalname); // REMOVE STR_REPLACE HERE
        $imgsizes = $path->getSize();
        $mimetype = $image->getClientMimeType();

        $picture = new Avatar();
        $picture->name = $request->name;
        $picture->filename = str_replace('\', '/', $path); // USE IT HERE
        $picture->save();
        $data->name = $request->name;
        return redirect('avatars')->with('successfully created or uploaded image');
    }

        public function destroy($id)
    {
        $data = Avatar::find($id);
        $data->delete();
        return redirect('/avatars');
    }
0 likes
5 replies
Neeraj1005's avatar
Neeraj1005
OP
Best Answer
Level 9

@sti3bas I found the answer. Basically My image is stored in public/uploads/media folder. So storage is not working for me. Thank you answering this thread.

    public function destroy($id)
    {
        $data = Avatar::find($id);
        $image_path = public_path().'/'.$data->filename;
        unlink($image_path);
        $data->delete();
        // if(Storage::delete($data->filename)) {
        //     $data->delete();
        //  }
        return redirect('/avatars');
    }
5 likes
puji_anjing's avatar

@Neeraj1005

$file_path = Photo::findOrFail($id);
$photo = $file_path->photo;
Storage::disk('public')->delete($photo);

Please or to participate in this conversation.