πŸš€ DevOps & SRE Certification Program πŸ“… Starting: 1st of Every Month 🀝 +91 8409492687 πŸ” Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

How to Remove Old Images of Users in Laravel?

How to Remove Old Images of Users in Laravel?

Uploading Profile Image of Users Click

Step 1. Go to app/Http/Controllers/HomeController.php file and define the deleteOldImage function.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Auth;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('home');
}
public function upload(Request $request)
{
if($request->hasFile('image')){
$filename = $request->image->getClientOriginalName();
$this->deleteOldImage();
$request->image->storeAs('images',$filename,'public');
Auth()->user()->update(['image'=>$filename]);
return back()->with('message','Profile Picture Uploaded Successfully');
}
return redirect()->back();
}
protected function deleteOldImage()
{
if (auth()->user()->image){
Storage::delete('/public/images/'.auth()->user()->image);
}
}
}

Step 2. Use Storage class file.

use Illuminate\Support\Facades\Storage;

Step 3. Write the success message code within home.blade.php file.

@if(session()->get('message'))
<div class="alert alert-success" role="alert">
<strong>Success: </strong>{{session()->get('message')}}
</div>
@endif

After uploading new Profile Image, the old one Image would be deleted from storage file.

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.