Update User Profile
This commit is contained in:
parent
d279d7cebd
commit
326bbfd7ac
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Http\Request;
|
||||
|
@ -25,14 +26,12 @@ public function update_profile(Request $request)
|
|||
'required' => ':attribute harus diisi.',
|
||||
'email' => ':attribute harus berupa email yang valid.',
|
||||
'min' => 'panjang :attribute minimal :min karakter.',
|
||||
]);
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => Str::ucfirst($validator->errors()->first()),
|
||||
'data' => null
|
||||
]);
|
||||
return redirect('user-profile')
|
||||
->withErrors($validator)
|
||||
->withInput();
|
||||
}
|
||||
|
||||
$user = User::findOrFail(Auth::user()->id);
|
||||
|
@ -46,41 +45,51 @@ public function update_profile(Request $request)
|
|||
|
||||
public function updatePassword(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
$request->validate([
|
||||
'password' => 'required|confirmed|min:5',
|
||||
$validator = Validator::make($request->all(), [
|
||||
'password' => 'required|min:5|max:255',
|
||||
'new_password' => 'required|min:5|max:255',
|
||||
], [
|
||||
'required' => ':attribute harus diisi.',
|
||||
'min' => 'panjang :attribute minimal :min karakter.',
|
||||
]);
|
||||
|
||||
$user->password = bcrypt($request->input('new_password'));
|
||||
$user->save();
|
||||
if ($validator->fails()) {
|
||||
return redirect('user-profile')
|
||||
->withErrors($validator)
|
||||
->withInput();
|
||||
}
|
||||
|
||||
return redirect()->back()->with('success', 'Password updated successfully.');
|
||||
|
||||
// $status = Password::reset(
|
||||
// $request->only('email', 'password', 'password_confirmation', 'token'),
|
||||
// function ($user, $password) {
|
||||
// $user->forceFill([
|
||||
// 'password' => bcrypt($password),
|
||||
// 'remember_token' => Str::random(60),
|
||||
// ])->save();
|
||||
// // Hapus token "remember me" setelah reset password
|
||||
// $user->tokens()->delete();
|
||||
// }
|
||||
// );
|
||||
|
||||
// return $status == Password::PASSWORD_RESET
|
||||
// ? redirect('/login')->with(['status' => __($status)])
|
||||
// : back()->withErrors(['email' => [__($status)]]);
|
||||
|
||||
// $user = Auth::user();
|
||||
|
||||
// $request->validate([
|
||||
// 'new_password' => 'required|string|min:8|confirmed',
|
||||
// ]);
|
||||
|
||||
// $user->password = bcrypt($request->input('new_password'));
|
||||
// $user->save();
|
||||
|
||||
// return redirect()->back()->with('success', 'Password updated successfully.');
|
||||
$user = User::findOrFail(Auth::user()->id);
|
||||
$user->update([
|
||||
'password' => $request->new_password,
|
||||
]);
|
||||
return redirect('user-profile')->with('success', 'Password Berhasil Diperbarui!');
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$user = Auth::user();
|
||||
return view('user-profile', compact('user'));
|
||||
}
|
||||
|
||||
public function updateProfilePicture(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'profile_picture' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
|
||||
]);
|
||||
|
||||
// Process the uploaded file and update the user's profile picture
|
||||
$user = Auth::user();
|
||||
|
||||
// Store the uploaded file and update the user's profile picture
|
||||
if ($request->hasFile('profile_picture')) {
|
||||
$imagePath = $request->file('profile_picture')->store('profile_pictures', 'public');
|
||||
$user->profile_picture = $imagePath;
|
||||
$user->save();
|
||||
}
|
||||
|
||||
// Redirect back or return a response
|
||||
return redirect()->route('user-profile')->with('success', 'Profile picture updated successfully.');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@ public function up(): void
|
|||
$table->timestamp('email_verified_at')->nullable();
|
||||
$table->string('password');
|
||||
$table->string('role');
|
||||
$table->string('profile_picture')->nullable();
|
||||
$table->rememberToken();
|
||||
$table->timestamps();
|
||||
});
|
||||
|
|
|
@ -15,7 +15,7 @@ public function up(): void
|
|||
$table->id();
|
||||
$table->string('nama');
|
||||
$table->string('prioritas');
|
||||
$table->string('bobot');
|
||||
$table->string('bobot')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
|
|
@ -3,6 +3,9 @@
|
|||
namespace Database\Seeders;
|
||||
|
||||
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
|
||||
use App\Models\Criteria;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class DatabaseSeeder extends Seeder
|
||||
|
@ -12,11 +15,24 @@ class DatabaseSeeder extends Seeder
|
|||
*/
|
||||
public function run(): void
|
||||
{
|
||||
// \App\Models\User::factory(10)->create();
|
||||
User::create([
|
||||
'name' => 'Ananda Zakia Syahfitri',
|
||||
'username' => 'zakia',
|
||||
'email' => 'anandazakia7@gmail.com',
|
||||
'password' => bcrypt('12345')
|
||||
]);
|
||||
|
||||
User::factory(2)->create();
|
||||
|
||||
Criteria::create([
|
||||
'nama' => 'Minat',
|
||||
'prioritas' => '1',
|
||||
]);
|
||||
|
||||
Criteria::create([
|
||||
'nama' => 'Bakat',
|
||||
'prioritas' => '2',
|
||||
]);
|
||||
|
||||
// \App\Models\User::factory()->create([
|
||||
// 'name' => 'Test User',
|
||||
// 'email' => 'test@example.com',
|
||||
// ]);
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 19 KiB |
|
@ -43,7 +43,11 @@
|
|||
<div class="dropdown">
|
||||
<a class="dropdown-toggle" href="#" role="button" data-toggle="dropdown">
|
||||
<span class="user-icon">
|
||||
{{ strtoupper(substr(Auth::user()->username, 0, 2)) }}
|
||||
@if (Auth::user()->profile_picture)
|
||||
<img src="{{ asset('storage/' . Auth::user()->profile_picture) }}">
|
||||
@else
|
||||
<img src="{{ asset('vendors/images/user.png') }}">
|
||||
@endif
|
||||
</span>
|
||||
<span class="user-name"> Hi, {{ Auth::user()->username }}</span>
|
||||
</a>
|
||||
|
|
|
@ -38,21 +38,36 @@
|
|||
<a href="modal" data-toggle="modal" data-target="#modal" class="edit-avatar">
|
||||
<i class="fa fa-pencil"></i>
|
||||
</a>
|
||||
<img src="vendors/images/photo1.jpg" alt="" class="avatar-photo" />
|
||||
@if (Auth::user()->profile_picture)
|
||||
<img src="{{ asset('storage/' . Auth::user()->profile_picture) }}">
|
||||
@else
|
||||
<img src="{{ asset('vendors/images/user.png') }}">
|
||||
@endif
|
||||
<div class="modal fade" id="modal" tabindex="-1" role="dialog"
|
||||
aria-labelledby="modalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog modal-dialog-centered" role="document">
|
||||
<div class="modal-content">
|
||||
<div class="modal-body pd-5">
|
||||
<div class="img-container">
|
||||
<img id="image" src="vendors/images/photo2.jpg" alt="Picture" />
|
||||
</div>
|
||||
<form action="{{ '/update-profile-picture' }}" method="post"
|
||||
enctype="multipart/form-data">
|
||||
@csrf
|
||||
<div class="img-container text-center">
|
||||
@if (Auth::user()->profile_picture)
|
||||
<img
|
||||
src="{{ asset('storage/' . Auth::user()->profile_picture) }}" class="img-fluid mx-auto mt-2" style="max-width: 100%; max-height: 200px;">
|
||||
@else
|
||||
<img src="{{ asset('vendors/images/user.png') }}" class="img-fluid mx-auto mt-2" style="max-width: 100%; max-height: 200px;">
|
||||
@endif
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<input type="submit" value="Update" class="btn btn-primary" />
|
||||
<button type="button" class="btn btn-default" data-dismiss="modal">
|
||||
Close
|
||||
</button>
|
||||
<input type="file" class="form-control-file" id="profile_picture" name="profile_picture" accept="image/*">
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="submit" class="btn btn-primary">Update</button>
|
||||
</form>
|
||||
<button type="button" class="btn btn-secondary"
|
||||
data-dismiss="modal">Close</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -82,37 +97,59 @@
|
|||
<!-- Profile -->
|
||||
<div class="tab-pane fade show active" id="profile" role="tabpanel">
|
||||
<div class="profile-setting">
|
||||
<form method="POST" action="{{ route('user-profile') }}" class="needs-validation" novalidate="">
|
||||
<form method="POST" action="{{ route('user-profile') }}"
|
||||
class="needs-validation" novalidate="">
|
||||
@csrf
|
||||
<h4 class=" text-center text-blue h5 mt-3 mb-0">Edit Your Personal Data</h4>
|
||||
<h4 class=" text-center text-blue h5 mt-3 mb-0">Edit Your Personal Data
|
||||
</h4>
|
||||
<ul class="profile-edit-list row">
|
||||
<li class="col-md-4">
|
||||
<div class="form-group">
|
||||
<label for="name">Name</label>
|
||||
<input id="name" type="text" class="form-control" name="name" value="{{ Auth::user()->name }}">
|
||||
<div class="invalid-feedback">Please fill in your name </div>
|
||||
<input id="name" type="text" class="form-control"
|
||||
name="name" value="{{ Auth::user()->name }}">
|
||||
<div class="invalid-feedback">Please fill in your name
|
||||
</div>
|
||||
</div>
|
||||
@if ($errors->has('name'))
|
||||
<div class="alert alert-danger">
|
||||
{{ $errors->first('name') }}
|
||||
</div>
|
||||
@endif
|
||||
</li>
|
||||
<li class="col-md-4">
|
||||
<div class="form-group">
|
||||
<label for="name">Username</label>
|
||||
<input id="name" type="text" class="form-control" name="username" value="{{ Auth::user()->username }}">
|
||||
<div class="invalid-feedback">Please fill in your username </div>
|
||||
<input id="name" type="text" class="form-control"
|
||||
name="username" value="{{ Auth::user()->username }}">
|
||||
<div class="invalid-feedback">Please fill in your username
|
||||
</div>
|
||||
</div>
|
||||
@if ($errors->has('username'))
|
||||
<div class="alert alert-danger">
|
||||
{{ $errors->first('username') }}</div>
|
||||
@endif
|
||||
</li>
|
||||
<li class="col-md-4">
|
||||
<div class="form-group">
|
||||
<label for="name">Email</label>
|
||||
<input id="name" type="text" class="form-control" name="email" value="{{ Auth::user()->email }}">
|
||||
<div class="invalid-feedback">Please fill in your email </div>
|
||||
<input id="name" type="text" class="form-control"
|
||||
name="email" value="{{ Auth::user()->email }}">
|
||||
<div class="invalid-feedback">Please fill in your email
|
||||
</div>
|
||||
</div>
|
||||
@if ($errors->has('email'))
|
||||
<div class="alert alert-danger">
|
||||
{{ $errors->first('email') }}</div>
|
||||
@endif
|
||||
</li>
|
||||
</ul>
|
||||
<div class="col-sm-12">
|
||||
<div class="input-group mb-3 mx-auto">
|
||||
<button class="btn btn-primary btn-lg btn-block" type="submit">Save Changes</button>
|
||||
</div>
|
||||
<div class="col-sm-12">
|
||||
<div class="input-group mb-3 mx-auto">
|
||||
<button class="btn btn-primary btn-lg btn-block"
|
||||
type="submit">Save Changes</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -122,28 +159,42 @@
|
|||
<div class="profile-setting">
|
||||
<form method="POST" action="{{ route('update-password') }}">
|
||||
@csrf
|
||||
<h4 class=" text-center text-blue h5 mt-3 mb-0">Edit Your Personal Password</h4>
|
||||
<h4 class=" text-center text-blue h5 mt-3 mb-0">Edit Your Personal
|
||||
Password</h4>
|
||||
<ul class="profile-edit-list row">
|
||||
<li class="col-md-6">
|
||||
<div class="form-group">
|
||||
<label for="password">Current Password</label>
|
||||
<input id="password" type="text" class="form-control" name="password" value="{{ Auth::user()->password }}" readonly>
|
||||
<div class="invalid-feedback">Please fill in your current password </div>
|
||||
<input id="password" type="text" class="form-control"
|
||||
name="password">
|
||||
<div class="invalid-feedback">Please fill in your current
|
||||
password </div>
|
||||
</div>
|
||||
@if ($errors->has('password'))
|
||||
<div class="alert alert-danger">
|
||||
{{ $errors->first('password') }}</div>
|
||||
@endif
|
||||
</li>
|
||||
<li class="col-md-6">
|
||||
<div class="form-group">
|
||||
<label for="new_password">New Password</label>
|
||||
<input id="new_password" type="text" class="form-control" name="new_password">
|
||||
<div class="invalid-feedback">Please fill in your new password </div>
|
||||
<input id="new_password" type="text"
|
||||
class="form-control" name="new_password">
|
||||
<div class="invalid-feedback">Please fill in your new
|
||||
password </div>
|
||||
</div>
|
||||
@if ($errors->has('new_password'))
|
||||
<div class="alert alert-danger">
|
||||
{{ $errors->first('new_password') }}</div>
|
||||
@endif
|
||||
</li>
|
||||
</ul>
|
||||
<div class="col-sm-12">
|
||||
<div class="input-group mb-3 mx-auto">
|
||||
<button class="btn btn-primary btn-lg btn-block" type="submit">Save Changes</button>
|
||||
</div>
|
||||
<div class="col-sm-12">
|
||||
<div class="input-group mb-3 mx-auto">
|
||||
<button class="btn btn-primary btn-lg btn-block"
|
||||
type="submit">Save Changes</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -90,6 +90,9 @@
|
|||
})->name('user-profile');
|
||||
Route::post('/user-profile', [UserController::class, 'update_profile']);
|
||||
|
||||
Route::post('/update-profile-picture', [UserController::class, 'updateProfilePicture'])
|
||||
->name('update-profile-picture');
|
||||
|
||||
Route::post('/update-password', [UserController::class, 'updatePassword'])->name('update-password');
|
||||
|
||||
Route::resource('kriteria', CriteriaController::class);
|
||||
|
|
Loading…
Reference in New Issue