As user management is part of commonly used applications, Let's see how can we implement complete User Management.
After Laravel Authentication we have User Model present in app\Models Folder,
The very first thing we need is User Resource Controller, to create resource controller run the command below.
php artisan make:controller UserController --resource
this command will create a Controller file in app\Http\Controllers Folder.
Next we have to register resource routes, to do so we have to define in routes\web.php file.
Route::resource('users', App\Http\Controllers\UserController::class);
Next we will create view files, let's add a folder 'users' inside resources\views\, and add a file list.blade.php
@extends('layouts.master')
@section('content')
<div class="container-fluid">
<!-- Page Heading -->
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0 text-gray-800">Users</h1>
<a href="{{route('users.create')}}" class="btn btn-sm btn-primary" >
<i class="fas fa-plus"></i> Add New
</a>
</div>
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">All Users</h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach ($users as $user)
<tr>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
@endsection
Now change the index function of UserController to
public function index()
{
$users = User::select('id', 'email', 'name')->get();
return view('users.list')->with([
'users' => $users
]);
}
Now on users route it display list of users available in users table.
Let's add a new view file for creating a new user inside resources\views\users name it add.blade.php
@extends('layouts.master')
@section('content')
<div class="container-fluid">
<!-- Page Heading -->
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0 text-gray-800">Add Users</h1>
<a href="{{route('users.index')}}" class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm"><i
class="fas fa-arrow-left fa-sm text-white-50"></i> Back</a>
</div>
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Add New User</h6>
</div>
<div class="card-body">
<form method="POST" action="">
@csrf
<div class="form-group row">
{{-- Name --}}
<div class="col-sm-6 mb-3 mb-sm-0">
<span style="color:red;">*</span>Name</label>
<input type="text" class="form-control form-control-user" id="exampleName"
placeholder="Name" required name="name" value="">
</div>
{{-- Email --}}
<div class="col-sm-6 mb-3 mb-sm-0">
<span style="color:red;">*</span>Email</label>
<input type="email" class="form-control form-control-user" id="exampleEmail"
placeholder="Email" required name="email" value="">
</div>
</div>
<button type="submit" class="btn btn-success btn-user btn-block">
Save
</button>
</form>
</div>
</div>
</div>
@endsection
Now return this view from UserController create function.
public function create()
{
return view('users.add');
}
It Will Load the Form for adding a new user.
To link sidebar menus to respective route we will add href attribute for those.
<a class="collapse-item" href="{{ route('users.index') }}">List</a>
<a class="collapse-item" href="{{ route('users.create')}}">Add</a>
You can access this code on TechTool India Github Repo.
You can watch the explanation video for more clarity.
In Next part i will explain about validation and store users data into database.
Read Next Part
Thank You for Reading
In case of any query related to laravel
Reach Out To me.
Twitter
Instagram
TechToolIndia
Top comments (0)