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

AlfaRiza
3 min readApr 1, 2022

Hello everyone, this article is the last part of the car rental admin series. In this part we will make additional features such as changing profile, changing password, dashboard display, and logging out.

Change Profile

first we will create a new controller to change the profile with the name ProfileController

php artisan make:controller ProfileController

then create a method profile and change profile

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');
}

then create view\profile\index.blade.php

@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

In ProfileController create 2 new methods with name password and 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');
}

create view profile\password.blade.php

@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

In this feature we will create a new controller with the name DashboardController with the invoice method, run the command

php artisan make:controller DashboardController -i

and in the invoke method add code like this

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'));
}

then in view\dashboard\index.blade.php add the following code for dashboard

<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

for this feature we simply add the following code to views\includes\sidebar.blade.php

<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>

The car rental admin application has been completed, to see the complete code, see https://github.com/AlfaRiza/rental-mobil.git
That is all and thank you :)

--

--