Laravel User Management - 3
Tags: laravel, programming, php, tutorial
Published: 2021-06-07 08:50:52

![Laravel User Management](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g6qrc5n94331biejy3aq.png)

In The last post i have explained about Create User, Form Validation, Try Catch, Database Transactions, Read More at [Laravel User Management - 2](https://dev.to/shanisingh03/laravel-user-management-2-1cgn)

In This post i am going to explain about User Edit and Delete in Laravel.

First We will add a new button in users list table
```php
<a href="{{ route('users.edit', ['user' => $user->id]) }}" class="btn btn-primary m-2">
    <i class="fa fa-pen"></i>
</a>
```

Now Let's add edit.blade.php inside users view.
```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">Edit 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">Edit User</h6>
        </div>
        <div class="card-body">
            <form method="POST" action="{{route('users.update', ['user' => $user->id])}}">
                @csrf
                @method('PUT')
                <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 @error('name') is-invalid @enderror" 
                            id="exampleName"
                            placeholder="Name" 
                            name="name" 
                            value="{{ old('name') ? old('name') : $user->name }}">

                        @error('name')
                            <span class="text-danger">{{$message}}</span>
                        @enderror
                    </div>


                    {{-- Email --}}
                    <div class="col-sm-6 mb-3 mb-sm-0">
                        <span style="color:red;">*</span>Email</label>
                        <input type="text" 
                            class="form-control form-control-user @error('email') is-invalid @enderror" 
                            id="exampleEmail"
                            placeholder="Email" 
                            name="email" 
                            value="{{ old('email') ? old('email') : $user->email }}">

                        @error('email')
                            <span class="text-danger">{{$message}}</span>
                        @enderror
                    </div>

                </div>

                {{-- Save Button --}}
                <button type="submit" class="btn btn-success btn-user btn-block">
                    Update
                </button>

            </form>
        </div>
    </div>

</div>


@endsection
```

Next Will update edit function of UserController
```php
public function edit($id)
{
    $user =  User::whereId($id)->first();

    if(!$user){
        return back()->with('error', 'User Not Found');
    }

    return view('users.edit')->with([
        'user' => $user
    ]);
}
```

Next Will Update the update Function of UserController
```php
public function update(Request $request, $id)
{
    $request->validate([
        'name' => 'required',
        'email' => 'required|email'
    ]);
    
    try {
        DB::beginTransaction();
        // Logic For Save User Data

        $update_user = User::where('id', $id)->update([
            'name' => $request->name,
            'email' => $request->email
        ]);

        if(!$update_user){
            DB::rollBack();

            return back()->with('error', 'Something went wrong while update user data');
        }

        DB::commit();
        return redirect()->route('users.index')->with('success', 'User Updated Successfully.');


    } catch (\Throwable $th) {
        DB::rollBack();
        throw $th;
    }
}
```

That's it Our Edit functionality is working fine, Let's Add Delete button on list users

```php
<td style="display: flex">
    <a href="{{ route('users.edit', ['user' => $user->id]) }}" class="btn btn-primary m-2">
        <i class="fa fa-pen"></i>
    </a>
    <form method="POST" action="{{ route('users.destroy', ['user' => $user->id]) }}">
        @csrf
        @method('DELETE')
        <button class="btn btn-danger m-2" type="submit">
            <i class="fa fa-trash"></i>
        </button>
    </form>
</td>
```

Next will Update Destroy Function in UserController

```php
public function destroy($id)
{
    try {
        DB::beginTransaction();

        $delete_user = User::whereId($id)->delete();

        if(!$delete_user){
            DB::rollBack();
            return back()->with('error', 'There is an error while deleting user.');
        }

        DB::commit();
        return redirect()->route('users.index')->with('success', 'User Deleted successfully.');



    } catch (\Throwable $th) {
        DB::rollBack();
        throw $th;
    }
}
```

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 3WyapmT_MWc %}

In Next part i will explain about edit and delete user.

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)