DEV Community

loading...
Cover image for Laravel User Management - 3

Laravel User Management - 3

shani singh
FullStack Developer, PHP, LARAVEL, VueJs, Angular.
・3 min read

Laravel User Management

In The last post i have explained about Create User, Form Validation, Try Catch, Database Transactions, Read More at Laravel User Management - 2

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

<a href="{{ route('users.edit', ['user' => $user->id]) }}" class="btn btn-primary m-2">
    <i class="fa fa-pen"></i>
</a>
Enter fullscreen mode Exit fullscreen mode

Now Let's add edit.blade.php inside users view.

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

Next Will update edit function of UserController

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

Next Will Update the update Function of UserController

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

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

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

Next will Update Destroy Function in UserController

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;
    }
}
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.

Thank You for Reading

In case of any query related to LARAVEL.
Reach Out To me.
Twitter
Instagram
TechToolIndia

Discussion (0)