Make a car rental admin using laravel 9 part 6: change profile, change password, display dashboard, and logout

Change Profile

php artisan make:controller ProfileController
public function profile(){
$data = Auth::user();
return view('profile.index', compact('data'));
}public function change_profile(Request $request){
$user = User::find(auth()->user()->id);
$data = $request->validate([
'name' => 'required',
'email' => 'required|email:rfc,dns',
]);
$user->update($data);
return redirect()->route('profile')->with('success', 'Data pelanggan berhasil diupdate');
}
@extends('layouts.main')
@section('content')
<h1 class="mt-4">Profile</h1>
<ol class="mb-4 breadcrumb">
<li class="breadcrumb-item active">Ubah Profile</li>
</ol>
<form action="{{ route('profile.change') }}" method="POST" enctype="multipart/form-data">
@csrf()
<div class="mb-3">
<label for="name" class="form-label">Nama</label>
<input type="text" class="form-control @error('name') is-invalid @enderror" id="name" name="name" value="{{ old('name', $data->name) }}">
@error('name')
<span class="invalid-feedback">{{ $message }}</span>
@enderror
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" required class="form-control @error('email') is-invalid @enderror" name="email" id="email" value="{{ old('email', $data->email) }}">
@error('email')
<span class="invalid-feedback">
{{ $message }}
</span>
@enderror
</div>
<div class="d-flex justify-content-end">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
@endsection

Change Password

public function password(){
return view('profile.password');
}public function change_password(Request $request){
$data = $request->validate([
'old_password' => 'required',
'password' => 'required|confirmed',
]);
if(Hash::check($request->old_password, auth()->user()->password)){
return redirect()->route('change-password')->with('error', 'Password lama tidak cocok');
}
$user = User::find(auth()->user()->id);
$user->update([
'password' => Hash::make($request->password)]);
return redirect()->route('change-password');
}
@extends('layouts.main')@section('content')
<h1 class="mt-4">Profile</h1>
<ol class="mb-4 breadcrumb">
<li class="breadcrumb-item active">Ubah Password</li>
</ol>
@if(session()->has('error'))
<div class="alert alert-danger" role="alert">
{{ session('error') }}
</div>
@endif
<form action="{{ route('change-password.change') }}" method="POST" enctype="multipart/form-data">
@csrf()
<div class="mb-3">
<label for="old_password" class="form-label">Password Lama</label>
<input type="password" required class="form-control @error('old_password') is-invalid @enderror" name="old_password" id="old_password" >
@error('old_password')
<span class="invalid-feedback">
{{ $message }}
</span>
@enderror
</div>
<div class="mb-3">
<label for="password" class="form-label">Password Baru</label>
<input type="password" required class="form-control @error('password') is-invalid @enderror" name="password" id="password" >
@error('password')
<span class="invalid-feedback">
{{ $message }}
</span>
@enderror
</div>
<div class="mb-3">
<label for="password_confirmation" class="form-label">Password Confirmation</label>
<input type="password" required class="form-control @error('password_confirmation') is-invalid @enderror" name="password_confirmation" id="password_confirmation">
@error('password_confirmation')
<span class="invalid-feedback">
{{ $message }}
</span>
@enderror
</div>
<div class="d-flex justify-content-end">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
@endsection

Dashboard

php artisan make:controller DashboardController -i
public function __invoke(Request $request){
$car = Car::count();
$user = User::where('is_admin', '!=', 1)->count();
$transaction = Transaction::count();
return view('dashboard.index', compact('car', 'user', 'transaction'));
}
<div class="row">
<div class="col-xl-4 col-md-6">
<div class="mb-4 text-white card bg-primary">
<div class="card-body">{{ $car }} Mobil</div>
<div class="card-footer d-flex align-items-center justify-content-between">
<a class="text-white small stretched-link" href="{{ route('car.index') }}">View Details</a>
<div class="text-white small"><i class="fas fa-angle-right"></i></div>
</div>
</div>
</div>
<div class="col-xl-4 col-md-6">
<div class="mb-4 text-white card bg-warning">
<div class="card-body">{{ $user }} Pelanggan</div>
<div class="card-footer d-flex align-items-center justify-content-between">
<a class="text-white small stretched-link" href="{{ route('user.index') }}">View Details</a>
<div class="text-white small"><i class="fas fa-angle-right"></i></div>
</div>
</div>
</div>
<div class="col-xl-4 col-md-6">
<div class="mb-4 text-white card bg-success">
<div class="card-body">{{ $transaction }} Transaksi</div>
<div class="card-footer d-flex align-items-center justify-content-between">
<a class="text-white small stretched-link" href="{{ route('transaction.index') }}">View Details</a>
<div class="text-white small"><i class="fas fa-angle-right"></i></div>
</div>
</div>
</div>
</div>

Logout

<a class="nav-link" href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
<div class="sb-nav-link-icon"><i class="fas fa-sign-out-alt"></i
</div>
Logout
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
M Alfa Riza

M Alfa Riza

FullStack Developer | Vue js | React js | Laravel