Laravel User Management - 2
Tags: laravel, programming, php, tutorial
Published: 2021-06-06 06:22:41

![Add User Form Validation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/btew4yfp2qw1bqxl8t34.png)

In The last post i have explained about List User and Create user form, Read More at [Laravel User Management - 1](https://dev.to/shanisingh03/laravel-user-management-1-5ha1)

In This post i am going to explain about Laravel Form Validation, try catch block, Database Transactions.

Change the form action to store method of User Controller
```php
<form method="POST" action="{{route('users.store')}}">
```

Next Will create validation logic in UserController.

```php
$request->validate([
     'name' => 'required',
     'email' => 'required|email'
]);
```

Next will print the error messages on create form.

```php
<form method="POST" action="{{route('users.store')}}">
    @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 @error('name') is-invalid @enderror" 
                id="exampleName"
                placeholder="Name" 
                name="name" 
                value="{{ old('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') }}">

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

    </div>

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

</form>
```

Now Add Try Catch and Database transactions in Controller
```php
public function store(Request $request)
{
    $request->validate([
        'name' => 'required',
        'email' => 'required|email'
    ]);
    
    try {
        DB::beginTransaction();
        // Logic For Save User Data

        $create_user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make('password')
        ]);

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

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

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


    } catch (\Throwable $th) {
        DB::rollBack();
        throw $th;
    }
}
```
Now To print success message we need to add below code in list blade file.
```php
@if (session('success'))
<span class="text-success">{{ session('success') }}</span>
@endif
```

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 KvQtgxQDzw4 %}

In Next part i will explain about edit and delete user.
[Read Next Part](https://dev.to/shanisingh03/laravel-user-management-3-2g7h)


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)