Current File : /home/tradevaly/www/app/Http/Controllers/User/ProfileController.php
<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\User;
use Brian2694\Toastr\Facades\Toastr;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Mail;
use Illuminate\Support\Facades\Hash;


class ProfileController extends Controller
{
    
public function profile(Request $request){
       if($request->isMethod('post')){
            $request->validate([
                'name' => 'required',
            ]);
            $id= Auth::id();
            $user= User::find($id);
            
            if($request->email != $user->email){
                 $request->validate([
                'email' => 'required|unique:users,email',
            ]);
            $user->email = $request->email;
            Mail::to($request->email)->send(new \App\Mail\MailChange($user));
            }
            if ($request->hasFile('image')) {

                $image = $request->file('image');
                $currentDate = Carbon::now()->toDateString();
                $imageName = $currentDate . '-' . uniqid() . '.' . $image->getClientOriginalExtension();
                if (!Storage::disk('public')->exists('images/user/avatar/' . $user->image)) {
                    Storage::disk('public')->makeDirectory('images/user/avatar/' . $user->image);
                }
                $userImage = Image::make($image)->resize(100, 100)->stream();
                Storage::disk('public')->put('images/user/avatar/' . $imageName, $userImage);
                $user->image = $imageName;
            }
                $user->name = $request->name;
                
                $user->save();
            Toastr::success('Information Updated Successfully!!');
             return back();die;
        }
        return view('user.pages.profile');
    }
    public function password(Request $request){
        $id= Auth::user()->id;
        $user= User::find($id);
        if($request->isMethod('post')){
            $data=$request->all();
           if($data['old']==""){
            Toastr::error('Please enter current password!');
            return redirect()->back();die;
           }
          elseif(!Hash::check($data['old'],$user['password'])){
             Toastr::error('Current password is incorrect!');
             return redirect()->back();die;
           } elseif(!Hash::check($data['new'],$user['confirm'])){
             Toastr::error('Password do not matcht!');
             return redirect()->back();die;
           }
           else{
             $user->update(['password'=>Hash::make($data['confirm'])]);
             Toastr::success('Password updated!');
             return redirect()->back();;
           }
        }
        return view('user.pages.reset')->with(compact('user'));
    }

}