Current File : /home/tradevaly/www/app/Http/Controllers/Admin/ProfileController.php |
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Hash;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;
use App\Models\Admin;
use Carbon\Carbon;
class ProfileController extends Controller
{
public function index(){
return view('backend.pages.profile.index');
}
public function update(Request $request){
$request->validate([
'name' => 'required',
'phone' => 'required',
]);
$id = Auth::guard('admin')->user()->id;
$user = Admin::find($id);
if($request->hasFile('image'))
{
$image=$request->file('image');
$currentDate=Carbon::now()->toDateString();
$imageName=$currentDate.'-'.uniqid().$image->getClientOriginalExtension();
if(!Storage::disk('public')->exists('images/admin/avatar'))
{
Storage::disk('public')->makeDirectory('images/admin/avatar');
}
$profileImage = Image::make($image)->resize(400,400)->stream();
Storage::disk('public')->put('images/admin/avatar/'.$imageName,$profileImage);
$user->image = $imageName;
}
$user->name = $request->name;
$user->phone = $request->phone;
$user->save();
Toastr::success('Profile Updated Successfully!');
return back();
}
public function reset(){;
$user= Admin::find(Auth::guard('admin')->user()->id);
return view('backend.pages.profile.reset',compact('user'));
}
public function password_update(Request $request)
{
$user = Admin::find(Auth::guard('admin')->user()->id);
$request->validate([
'old' => 'required',
'new' => 'required|min:6',
'confirm' => 'required|same:new',
], [
'old.required' => 'Please enter the current password!',
'new.required' => 'Please enter the new password!',
'new.min' => 'The new password must be at least 6 characters long.',
'confirm.required' => 'Please confirm the new password!',
'confirm.same' => 'Passwords do not match!',
]);
if (!Hash::check($request->old, $user->password)) {
Toastr::error('Current password is incorrect!');
return redirect()->back();
}
$user->update(['password' => Hash::make($request->new)]);
Toastr::success('Password Changed Successfully!');
return redirect()->back();
}
}