Skip to content

Commit

Permalink
add user profile update
Browse files Browse the repository at this point in the history
  • Loading branch information
subhadipghorui committed Dec 17, 2020
1 parent 2fa8c51 commit 2d72157
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 2 deletions.
90 changes: 90 additions & 0 deletions app/Http/Controllers/User/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
namespace App\Http\Controllers\User;

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

class DashboardController extends Controller
{
Expand All @@ -15,4 +21,88 @@ public function index()
public function likedPosts(){
return view('user.likedPosts');
}
public function showProfile()
{
$user = User::find(Auth::user()->id);
return view('user.profile', compact('user'));
}
public function updateProfile(Request $request)
{
// Fix the update issue
if ($request->name == User::findOrFail(Auth::id())->name) {
$this->validate($request, [
'name' => 'required',
'userid' => 'required',
'about' => 'sometimes|max:255',
'image' => 'sometimes|image|mimes:jpg,png,bmp,jpeg|max:2000'

]);
} else {
$this->validate($request, [
'name' => 'required',
'userid' => 'required|unique:users',
'about' => 'sometimes|max:255',
'image' => 'sometimes|image|mimes:jpg,png,bmp,jpeg|max:2000'

]);
}
$user = User::findOrFail(Auth::user()->id);
if ($request->image != null) {
// Image
$image = $request->image;
$imageName = Str::slug($request->name, '-') . uniqid() . '.' . $image->getClientOriginalExtension();

if (!Storage::disk('public')->exists('user')) {
Storage::disk('public')->makeDirectory('user');
}
// Delete old Image
if ($user->image !== 'default.jpg' && Storage::disk('public')->exists('user/' . $user->image)) {
Storage::disk('public')->delete('user/' . $user->image);
}
// Store
// $image->storeAs('user', $imageName, 'public');
$userImg = Image::make($image)->fit(200, 200)->stream();
Storage::disk('public')->put('user/' . $imageName, $userImg); //The put method may be used to store raw file contents on a disk
} else {
$imageName = $user->image;
}

$user->name = $request->name;
$user->userid = $request->userid;
$user->image = $imageName;
$user->about = $request->about;
$user->save();
Toastr::success('Details Successfully Saved', 'Success');

return redirect()->back();
}

public function changePassword(Request $request)
{
$this->validate($request, [
'old_password' => 'required',
'password' => 'required|max:255|confirmed'
]);

// Cross check the old password
$oldPass = Auth::user()->password; // hashed
if (Hash::check($request->old_password, $oldPass)) {
// old pass shoud not be same as new pass
if (!Hash::check($request->password, $oldPass)) {
$user = User::find(Auth::id());
$user->password = Hash::make($request->password);
$user->save();

// Logout
Auth::logout();
return redirect()->back();
} else {
Toastr::error('New password should not be same as old password :(');
return redirect()->back();
}
} else {
Toastr::error('Enter the correct old password :(');
return redirect()->back();
}
}
}
10 changes: 9 additions & 1 deletion resources/views/layouts/backend/partials/headermenu.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@ class="user-avatar rounded-circle"
</a>

<div class="user-menu dropdown-menu">
@if (Auth::user()->role->id == 1)
<a class="nav-link" href="{{route('admin.profile')}}"
><i class="fa fa-user"></i> My Profile</a
>

@elseif(Auth::user()->role->id == 2)
<a class="nav-link" href="{{route('user.profile')}}"
><i class="fa fa-user"></i> My Profile</a
>
@else
null
@endif

<a class="nav-link" href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Expand Down
3 changes: 2 additions & 1 deletion resources/views/layouts/frontend/partials/navbar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
<i class="fa fa-user-circle" aria-hidden="true"></i>&nbsp;
</a>
<div class="dropdown-menu menu1" style="display: none" id="dropMenu">
<a href="{{route('admin.profile')}}" class="dropdown-item" target="_blank"> <i class="fa fa-user-circle" aria-hidden="true"></i>&nbsp;{{Auth::user()->name}}</a>
@if (Auth::user()->role->id == 1)
<a href="{{route('admin.profile')}}" class="dropdown-item" target="_blank"> <i class="fa fa-user-circle" aria-hidden="true"></i>&nbsp;{{Auth::user()->name}}</a>
<a class="dropdown-item" href="{{ route('admin.dashboard') }}"><i class="fa fa-tv" aria-hidden="true"></i>&nbsp; Dashboard</a>
@elseif(Auth::user()->role->id == 2)
<a href="{{route('user.profile')}}" class="dropdown-item" target="_blank"> <i class="fa fa-user-circle" aria-hidden="true"></i>&nbsp;{{Auth::user()->name}}</a>
<a class="dropdown-item" href="{{ route('user.dashboard') }}"><i class="fa fa-tv" aria-hidden="true"></i>&nbsp; Dashboard</a>
<a class="dropdown-item" href="{{route('user.like.posts')}}"><i class="fa fa-heart" aria-hidden="true"></i>&nbsp; Favorite List</a>
@else
Expand Down
138 changes: 138 additions & 0 deletions resources/views/user/profile.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
@extends('layouts.backend.app')
@push('header')
<link rel="stylesheet" href="http://cdn.bootcss.com/toastr.js/latest/css/toastr.min.css">
@endpush
@section('content')
<div class="breadcrumbs">
<div class="col-sm-4">
<div class="page-header float-left">
<div class="page-title">
<h1>Dashboard</h1>
</div>
</div>
</div>
<div class="col-sm-8">
<div class="page-header float-right">
<div class="page-title">
<ol class="breadcrumb text-right">
<li>Dashboard</li>
<li class="active">Profile</li>
</ol>
</div>
</div>
</div>
</div>

<div class="content mt-3">
<div class="row">
<div class="col-md-12">
@if ($errors->any())

@foreach ($errors->all() as $error)
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<span class="badge badge-pill badge-danger">Erorr</span> {{$error}}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
@endforeach

@endif

</div>
<div class="col-md-4">
<div class="card">
<div class="card-header">
<strong class="card-title mb-3">Profile Card</strong>
</div>
<div class="card-body">
<div class="mx-auto d-block">
<img class="rounded-circle mx-auto d-block" src="{{asset('storage/user/'.$user->image)}}" alt="Card image cap">
<h5 class="text-sm-center mt-2 mb-1">{{$user->name}}</h5>
</div>
<hr>
</div>
</div>
</div>
<div class="col-md-8">
<div class="card">
<div class="card-header">
<h4>Default Tab</h4>
</div>
<div class="card-body">
<div class="default-tab">
<nav>
<div class="nav nav-tabs" id="nav-tab" role="tablist">
<a class="nav-item nav-link active show" id="nav-home-tab" data-toggle="tab" href="#nav-home" role="tab" aria-controls="nav-home" aria-selected="false">Profile</a>
<a class="nav-item nav-link" id="nav-profile-tab" data-toggle="tab" href="#nav-profile" role="tab" aria-controls="nav-profile" aria-selected="false">Credential</a>
</div>
</nav>
<div class="tab-content pl-3 pt-2" id="nav-tabContent">
<div class="tab-pane fade active show" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
<form action="{{route('user.profile.update')}}" method="POST" enctype="multipart/form-data" class="form-horizontal">
@csrf()
@method('PUT')
<div class="row form-group">
<div class="col col-md-3"><label class=" form-control-label">Email</label></div>
<div class="col-12 col-md-9">
<p class="form-control-static">{{$user->email}}</p>
</div>
</div>
<div class="row form-group">
<div class="col col-md-3"><label for="userid" class=" form-control-label">User Id</label></div>
<div class="col-12 col-md-9"><input type="text" id="userid" name="userid" placeholder="Text" class="form-control" value="{{$user->userid}}">
</div>
</div>
<div class="row form-group">
<div class="col col-md-3"><label for="name" class=" form-control-label">Name</label></div>
<div class="col-12 col-md-9"><input type="text" id="name" name="name" placeholder="Text" class="form-control" value="{{$user->name}}"></div>
</div>
<div class="row form-group">
<div class="col col-md-3"><label for="file-input" class=" form-control-label">Profile image</label></div>
<div class="col-12 col-md-9"><input type="file" id="file-input" name="image" class="form-control-file"></div>
</div>
<div class="row form-group">
<div class="col col-md-3"><label for="about" class=" form-control-label">About</label></div>
<div class="col-12 col-md-9"><textarea name="about" id="about" rows="4" placeholder="Content..." class="form-control">{{$user->about}}</textarea></div>
</div>
<button type="submit" class="btn btn-primary btn-sm">
<i class="fa fa-dot-circle-o"></i> Submit
</button>
</form>
</div>
<div class="tab-pane fade" id="nav-profile" role="tabpanel" aria-labelledby="nav-profile-tab">
<form action="{{route('user.profile.password')}}" method="POST" enctype="multipart/form-data" class="form-horizontal">
@csrf
@method('PUT')
<div class="row form-group">
<div class="col col-md-3"><label for="old_password" class=" form-control-label">Old Password</label></div>
<div class="col-12 col-md-9"><input type="password" id="old_password" name="old_password" placeholder="Password" class="form-control"></div>
</div>
<div class="row form-group">
<div class="col col-md-3"><label for="password" class=" form-control-label">New Password</label></div>
<div class="col-12 col-md-9"><input type="password" id="password" name="password" placeholder="Password" class="form-control"></div>
</div>
<div class="row form-group">
<div class="col col-md-3"><label for="password_confirmation" class=" form-control-label">Confirm Password</label></div>
<div class="col-12 col-md-9"><input type="password" id="password_confirmation" name="password_confirmation" placeholder="Password" class="form-control"></div>
</div>
<button type="submit" class="btn btn-primary btn-sm">
<i class="fa fa-dot-circle-o"></i> Submit
</button>
</form>
</div>
</div>

</div>
</div>
</div>
</div>
</div>
</div>
<!-- .content -->
@endsection
@push('footer')
<script src="http://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/toastr.js/latest/js/toastr.min.js"></script>
{!! Toastr::message() !!}
@endpush
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
// User ////////////////////////////////////////////////////////////////////////
Route::group(['prefix' => 'user', 'as' => 'user.', 'namespace' => 'User', 'middleware' => ['auth', 'user']], function () {
Route::get('dashboard', 'DashboardController@index')->name('dashboard');
Route::get('profile', 'DashboardController@showProfile')->name('profile');
Route::put('profile', 'DashboardController@updateProfile')->name('profile.update');
Route::put('profile/password', 'DashboardController@changePassword')->name('profile.password');
Route::get('comments', 'CommentController@index')->name('comment.index');
Route::delete('/comment/{id}', 'CommentController@destroy')->name('comment.destroy');
Route::get('/reply-comments', 'CommentReplyController@index')->name('reply-comment.index');
Expand Down

0 comments on commit 2d72157

Please sign in to comment.