feat: implement profile editing and password change
This commit is contained in:
@@ -3,8 +3,10 @@
|
||||
namespace App\Http\Controllers\Account;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Account\EditProfileChangePasswordRequest;
|
||||
use App\Http\Requests\Account\EditProfileRequest;
|
||||
use App\Http\Requests\EditProfileUpdateRequest;
|
||||
use App\Models\City;
|
||||
use App\Models\Province;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
@@ -16,8 +18,14 @@ class EditProfileController extends Controller
|
||||
{
|
||||
$title = 'ویرایش حساب';
|
||||
$user = auth()->user();
|
||||
$provinces = Province::with('cities')->get();
|
||||
return view('account.edit-profile' , compact('title','user', 'provinces'));
|
||||
$provinces = Province::orderBy('name')->get();
|
||||
$selectedProvinceId = old('province_id', $user->province_id);
|
||||
|
||||
$cities = [];
|
||||
if ($selectedProvinceId) {
|
||||
$cities = City::where('province_id', $selectedProvinceId)->orderBy('name')->get();
|
||||
}
|
||||
return view('account.edit-profile', compact('title','user', 'provinces' , 'cities'));
|
||||
}
|
||||
|
||||
public function update(EditProfileUpdateRequest $request)
|
||||
@@ -28,13 +36,30 @@ class EditProfileController extends Controller
|
||||
'email',
|
||||
'mobile',
|
||||
'city_id',
|
||||
'province_id'
|
||||
'province_id',
|
||||
]);
|
||||
|
||||
|
||||
Auth::user()->update($inputs);
|
||||
|
||||
return redirect()->back();
|
||||
return redirect()->back()->with('success', 'پروفایل با موفقیت ویرایش شد.');
|
||||
}
|
||||
|
||||
public function changePassword(EditProfileChangePasswordRequest $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if (!Hash::check($request->current_password, $user->password)) {
|
||||
return back()
|
||||
->withErrors([
|
||||
'current_password' => 'رمز عبور فعلی اشتباه است.',
|
||||
]);
|
||||
}
|
||||
|
||||
$user->update([
|
||||
'password' => Hash::make($request->new_password),
|
||||
]);
|
||||
|
||||
return back()->with('success', 'رمز عبور با موفقیت تغییر کرد.');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
namespace App\Http\Controllers\Account;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\City;
|
||||
use App\Models\Province;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
@@ -13,6 +15,15 @@ class ProfileController extends Controller
|
||||
{
|
||||
$title = 'پروفایل';
|
||||
$user = Auth::user();
|
||||
|
||||
return view('account.profile', compact('title', 'user'));
|
||||
}
|
||||
|
||||
public function getCities($provinceId)
|
||||
{
|
||||
$cities = City::where('province_id', $provinceId)->orderBy('name')->get(['id', 'name']);
|
||||
return response()->json($cities);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Account;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class EditProfileChangePasswordRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'current_password' => [
|
||||
'required',
|
||||
'current_password'
|
||||
],
|
||||
'new_password' => [
|
||||
'required',
|
||||
'min:8',
|
||||
'max:255',
|
||||
'alpha_num',
|
||||
'confirmed'
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -251,7 +251,8 @@ return [
|
||||
'avatar' => 'تصویر',
|
||||
'province_id' => 'استان',
|
||||
'city_id' => 'شهر',
|
||||
'national_code' => 'کدملی'
|
||||
'national_code' => 'کدملی',
|
||||
'new_password' => 'رمزعبور جدید'
|
||||
|
||||
],
|
||||
|
||||
|
||||
@@ -8,14 +8,18 @@
|
||||
<div class="alert alert-success">{{ session('success') }}</div>
|
||||
</div>
|
||||
@endif
|
||||
@if (session('error'))
|
||||
<div class="col-lg-12 mb-3">
|
||||
<div class="alert alert-danger">{{ session('error') }}</div>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<div class="col-lg-12 mb-4">
|
||||
<div class="user-profile-card">
|
||||
<h4 class="user-profile-card-title">بروزرسانی مشخصات پروفایل</h4>
|
||||
<div class="user-profile-form">
|
||||
<form action="{{route('accounts.edit-profile.update' , $user->id)}}" method="POST">
|
||||
<form action="{{ route('accounts.edit-profile.update') }}" method="POST">
|
||||
@csrf
|
||||
@method('PUT')
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<div class="form-group">
|
||||
@@ -60,7 +64,7 @@
|
||||
<div class="col-md-6">
|
||||
<div class="form-group">
|
||||
<label>استان</label>
|
||||
<select name="province_id" class="form-control">
|
||||
<select name="province_id" id="province_id" class="form-control">
|
||||
<option value="">انتخاب استان</option>
|
||||
@foreach($provinces as $province)
|
||||
<option value="{{ $province->id }}"
|
||||
@@ -78,9 +82,9 @@
|
||||
<div class="col-md-6">
|
||||
<div class="form-group">
|
||||
<label>شهر</label>
|
||||
<select name="city_id" class="form-control">
|
||||
<select name="city_id" id="city_id" class="form-control">
|
||||
<option value="">انتخاب شهر</option>
|
||||
@foreach($province->cities as $city)
|
||||
@foreach($cities as $city)
|
||||
<option value="{{ $city->id }}"
|
||||
{{ old('city_id', $user->city_id) == $city->id ? 'selected' : '' }}>
|
||||
{{ $city->name }}
|
||||
@@ -92,6 +96,7 @@
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<button type="submit" class="theme-btn mt-4">آپدیت مشخصات پروفایل <i class="far fa-user"></i>
|
||||
</button>
|
||||
@@ -105,27 +110,34 @@
|
||||
<h4 class="user-profile-card-title">عوض کردن رمز عبور</h4>
|
||||
<div class="col-lg-12">
|
||||
<div class="user-profile-form">
|
||||
<form action="#" method="POST">
|
||||
<form action="{{ route('accounts.edit-profile.change-password') }}" method="POST">
|
||||
@csrf
|
||||
@method('PUT')
|
||||
<div class="form-group">
|
||||
<label>رمز عبور فعلی</label>
|
||||
<input type="password" name="current_password" class="form-control"
|
||||
placeholder="رمز عبور فعلی">
|
||||
@error('current_password') <span class="text-danger">{{ $message }}</span> @enderror
|
||||
@error('current_password')
|
||||
<span class="text-danger">{{ $message }}</span>
|
||||
@enderror
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label>رمز عبور جدید</label>
|
||||
<input type="password" name="new_password" class="form-control"
|
||||
placeholder="رمز عبور جدید">
|
||||
@error('new_password') <span class="text-danger">{{ $message }}</span> @enderror
|
||||
@error('new_password')
|
||||
<span class="text-danger">{{ $message }}</span>
|
||||
@enderror
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label>تکرار رمز عبور جدید</label>
|
||||
<input type="password" name="new_password_confirmation" class="form-control"
|
||||
placeholder="تکرار رمز عبور جدید">
|
||||
</div>
|
||||
<button type="submit" class="theme-btn mt-4">عوض کردن رمز عبور <i class="far fa-key"></i>
|
||||
|
||||
<button type="submit" class="theme-btn mt-4">
|
||||
عوض کردن رمز عبور <i class="far fa-key"></i>
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
@@ -134,3 +146,37 @@
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
@push('scripts')
|
||||
<script>
|
||||
$(document).ready(function () {
|
||||
$('#province_id').on('change', function () {
|
||||
let provinceId = $(this).val();
|
||||
let citySelect = $('#city_id');
|
||||
|
||||
|
||||
citySelect.html('<option value="">انتخاب شهر</option>');
|
||||
|
||||
|
||||
if (provinceId) {
|
||||
citySelect.html('<option value="">در حال بارگذاری شهرها...</option>');
|
||||
|
||||
$.ajax({
|
||||
url: "{{ url('/account/get-cities') }}/" + provinceId,
|
||||
type: "GET",
|
||||
dataType: "json",
|
||||
success: function (data) {
|
||||
let options = '<option value="">انتخاب شهر</option>';
|
||||
$.each(data, function (key, city) {
|
||||
options += '<option value="' + city.id + '">' + city.name + '</option>';
|
||||
});
|
||||
citySelect.html(options);
|
||||
},
|
||||
error: function () {
|
||||
citySelect.html('<option value="">خطا در دریافت لیست شهرها</option>');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
@endpush
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
@extends('layouts.app')
|
||||
|
||||
|
||||
@section('content')
|
||||
<main class="main">
|
||||
|
||||
<div class="site-breadcrumb" style="background: url({{asset('assets/img/breadcrumb/02.jpg')}})">
|
||||
<div class="container">
|
||||
<h2 class="breadcrumb-title">@yield('title')</h2>
|
||||
@@ -31,3 +29,4 @@
|
||||
|
||||
@endsection
|
||||
|
||||
|
||||
|
||||
@@ -86,5 +86,6 @@
|
||||
<script src="{{asset('assets/js/jquery.timepicker.min.js')}}"></script>
|
||||
<script src="{{asset('assets/js/wow.min.js')}}"></script>
|
||||
<script src="{{asset('assets/js/main.js')}}"></script>
|
||||
@stack('scripts')
|
||||
</body>
|
||||
</html>
|
||||
|
||||
+5
-1
@@ -21,11 +21,15 @@ Route::prefix('tours')->controller(TourController::class)->name('tours.')->group
|
||||
|
||||
|
||||
Route::prefix('account')->name('accounts.')->middleware('auth')->group(function (){
|
||||
Route::get('/profile' ,[ProfileController::class , 'index'])->name('profile');
|
||||
Route::controller(ProfileController::class)->group(function () {
|
||||
Route::get('/profile', 'index')->name('profile');
|
||||
Route::get('/get-cities/{province_id}', 'getCities')->name('get-cities');
|
||||
});
|
||||
|
||||
Route::prefix('edit-profile')->name('edit-profile.')->controller(EditProfileController::class)->group(function () {
|
||||
Route::get('/', 'index')->name('index');
|
||||
Route::post('/', 'update')->name('update');
|
||||
Route::post('change-password', 'changePassword')->name('change-password');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user