DEV Community

Cover image for Laravel User Management - 2
shani singh
shani singh

Posted on • Updated on

Laravel User Management - 2

Add User Form Validation

In The last post i have explained about List User and Create user form, Read More at Laravel User Management - 1

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

<form method="POST" action="{{route('users.store')}}">
Enter fullscreen mode Exit fullscreen mode

Next Will create validation logic in UserController.

$request->validate([
     'name' => 'required',
     'email' => 'required|email'
]);
Enter fullscreen mode Exit fullscreen mode

Next will print the error messages on create form.

<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>
Enter fullscreen mode Exit fullscreen mode

Now Add Try Catch and Database transactions in Controller

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Now To print success message we need to add below code in list blade file.

@if (session('success'))
<span class="text-success">{{ session('success') }}</span>
@endif
Enter fullscreen mode Exit fullscreen mode

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 edit and delete user.
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)