samedi 26 janvier 2019

How to delete old avatar after uploading new on laravel?

I was trying to delete old avatar after uploading new avatar. Just I couldn't find solution. I am new to laravel5, Could you help me?

    public function s_general(Request $request){
    $this->validate($request,[
        'username' => 'required|min:3|max:50',
        'avatar' => 'nullable|image|mimes:jpeg,png,jpg|max:1024',
        'fullname' => 'required',
        'email' => 'required|email'
    ]);
    if ($request['username'] == Auth::user()->username &&$request['fullname'] == Auth::user()->name && $request['email'] == Auth::user()->email && !$request->hasFile('avatar')) {
        return redirect()->back()->with('general_msg', Lang::get('trans.noChanges_MSG'));
    }else{
        $avatar = $request->file('avatar');
        if ($request->hasFile('avatar')) {
            $avatar_ext = $avatar->getClientOriginalExtension();
            $avatar_name = rand(9,999999999)+time().".".$avatar_ext;
            $avatar_new = $avatar->storeAs("avatar",$avatar_name);
        }else{
            $avatar_name = Auth::user()->avatar;
        }


        $update_general = User::where('uid',Auth::user()->uid)->update(['username' => $request['username'],'name' => $request['fullname'], 'email' => $request['email'],'avatar' => $avatar_name]);
        return redirect()->back()->with('general_msg', Lang::get('trans.changes_saved'));
    }

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire