Laravel User Management - 1
Tags: laravel, programming, php, tutorial
Published: 2021-06-01 01:45:04

As user management is part of commonly used applications, Let's see how can we implement complete User Management.

After [Laravel Authentication](https://dev.to/shanisingh03/laravel-8-authentication-7fb) 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
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.

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

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

```php
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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/01ec3pem49fs1ug5vwrz.png)

Let's add a new view file for creating a new user inside resources\views\users name it add.blade.php

```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.
```php
public function create()
{
   return view('users.add');
}
```

It Will Load the Form for adding a new user.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pkg9jpegrri29sa3z21w.png)

To link sidebar menus to respective route we will add href attribute for those.

```php
<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](https://github.com/TechTool-India/ExampleProject/tree/user-management).

You can watch the explanation video for more clarity.

{% youtube 6dUr28OqIhg %}

In Next part i will explain about validation and store users data into database.
[Read Next Part](https://dev.to/shanisingh03/laravel-user-management-2-1cgn)

Thank You for Reading

In case of any query related to laravel
Reach Out To me.
[Twitter](https://twitter.com/shanisingh03)
[Instagram](https://www.instagram.com/shanisingh03/)
[TechToolIndia](https://youtube.com/techtoolindia)