Requirement: Only admin can Create new Users. So we stop default registration by default by going to register Controller
public function __construct()
{
// $this->middleware('guest'); change this to
// $this->middleware('auth');
}
Steps to follow
- Create Routes & Point them to User Controller
- Create View Files
- Define Logic
Route::middleware('auth')->get('/registerUser', [App\Http\Controllers\DashboardController::class, 'register'])->name('registeruser');
Route::middleware('auth')->get('/listUsers', [App\Http\Controllers\DashboardController::class, 'listusers'])->name('listusers');
Route::middleware('auth')->post('/createUser/store', [App\Http\Controllers\DashboardController::class, 'createUser'])->name('createUser.store');
Route::middleware('auth')->get('/edituser/{id}', [App\Http\Controllers\DashboardController::class, 'edituser'])->name('edituser');
Route::middleware('auth')->post('/updateuser/{id}', [App\Http\Controllers\DashboardController::class, 'updateuser'])->name('updateuser');
use Illuminate\Support\Facades\Hash;
public function register() {
return view('dashboard.registeruser');
}
public function createUser(Request $request) {
// return $request;
$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = Hash::make($request->password);
$user->phone = $request->phone;
$user->save();
return redirect('/listusers')->with('status', 'User Created!');
}
public function listusers(Request $request) {
$users = User::all();
return view('dashboard.listusers', ['users' => $users]);
}
public function edituser($id) {
$user = \DB::table('users')->where('id', $id)->first();
return view('dashboard.updateuser', [
'user' => $user,
]);
}
public function updateuser($id, Request $request) {
// return $request;
$user = User::findOrFail($id);
$user->name = $request->name;
$user->email = $request->email;
if(!empty($request->password)) {
$user->password = Hash::make($request->password);
}
$user->phone = $request->phone;
$user->save();
}
Register User View Page
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register New User') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('createUser.store') }}">
@csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
@error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email">
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">
@error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
</div>
</div>
<div class="form-group row">
<label for="phone" class="col-md-4 col-form-label text-md-right">{{ __('Phone') }}</label>
<div class="col-md-6">
<input id="phone" type="text" class="form-control @error('phone') is-invalid @enderror" name="phone" value="{{ old('phone') }}" required>
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
List Users
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">{{ __('List Users') }} <a class="btn btn-success float-right" href="{{ url('/registeruser')}}">Add New user</a></div>
<div class="card-body">
<table class="table table-striped table-responsive table-bordered">
<thead>
<th>Action</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
</thead>
@foreach ($users as $list)
<tr>
<td>
<div class="btn-group" role="group" aria-label="Basic example">
<a href="{{ route('edituser', ['id' => $list->id]) }}" class="btn btn-primary btn-sm">Update User</a> </div>
</td>
<td>{{$list->name}}</td>
<td>{{$list->email}}</td>
<td>{{$list->phone}}</td>
</tr>
@endforeach
</table>
</div>
</div>
</div>
</div>
</div>
@endsection
Update User Profile
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Update User Profile') }} - <strong>{{$user->name}}</strong></div>
<div class="card-body">
<form method="POST" action="{{ route('updateuser', ['id' => $user->id]) }}">
@csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control @error('name') is-invalid @enderror" name="name" required autocomplete="name" value="{{$user->name}}">
@error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{$user->email }}" required autocomplete="email">
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Change Password, if required') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" autocomplete="new-password">
</div>
</div>
<div class="form-group row">
<label for="phone" class="col-md-4 col-form-label text-md-right">{{ __('Phone') }}</label>
<div class="col-md-6">
<input id="phone" type="text" class="form-control @error('phone') is-invalid @enderror" name="phone" value="{{ $user->phone }}" required>
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Update Profile') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection