Laravel Crud Operation - Update Data

Laravel Crud Operation - Update Data

In this article, we learn about the process of updating data in the Laravel crud operation. This is the third article on the Laravel crud operation.

Rays Coding

LARAVEL CRUD OPERATION-UPDATE DATA

First, let's see the list of data, which we will update in the Laravel crud operation.

RAYS CODING

LIST OF DATA


let's see the updating process

create two routes, with the help of the first we display the edit form, and with the help of the second route are used for updating data.

    Route::get('/updateform/{id}', [CrudController::class, 'updateview'])->name('updateview.data');
    Route::post('/updatesubmit/{id}', [CrudController::class, 'updatedate'])->name('updatesubmit.data');

1. Route for Update Form Display:

    Route::get('/updateform/{id}', [CrudController::class, 'updateview'])->name('updateview.data');

This code defines a GET route using Laravel's routing system. When a user accesses the URL `/updateform/{id}`, the application will invoke the `updateview` method of the `CrudController` class. The `{id}` is a dynamic parameter, meaning it represents a variable value that will be provided in the URL. The `->name('updateview.data')` part gives a name to this route, allowing you to reference it by name in other parts of your application.

2. Route for Handling Form Submission:

    Route::post('/updatesubmit/{id}', [CrudController::class, 'updatedate'])->name('updatesubmit.data');

This code defines a POST route for the URL `/updatesubmit/{id}`. When a user submits a form to this URL using the HTTP POST method, the application will invoke the `updatedate` method of the `CrudController` class. Similar to the first route, `{id}` is a dynamic parameter. The `->name('updatesubmit.data')` part assigns the name 'updatesubmit.data' to this route.

Edit Form

@extends('crud.layout')
@section('content')
    <div class="container mt-5 p-0">
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-header">
                        <h4>Crud Operation In Laravel - Edit</h4>
                    </div>
                    <div class="card-body">
                        @if (session('success'))
                            <div class="alert alert-success">
                                {{ session('success') }}
                            </div>
                        @endif
                        @if (session('error'))
                            <div class="alert alert-danger">
                                {{ session('error') }}
                            </div>
                        @endif
                        <form action="{{route('updatesubmit.data', ['id' => $update->id])}}" method="post" enctype="multipart/form-data" >
                            @csrf
                            {{-- row-1 --}}
                            <div class="row m-2">
                                <div class="col-lg-6">
                                    <input type="text" name="username"  class="form-control" autocomplete="off" placeholder="Enter Your Name" value="{{$update->name}}">
                                    @error('username')
                                        <div class="alert alert-danger">{{$message}}</div>
                                    @enderror
                                </div>
                                <div class="col-lg-6">
                                    <input type="email" name="useremail"  class="form-control" autocomplete="off" placeholder="Enter Your Email" value="{{$update->email}}">
                                    @error('useremail')
                                        <div class="alert alert-danger">{{$message}}</div>
                                    @enderror
                                </div>
                            </div>
                            {{-- row-2 --}}
                            <div class="row m-2">
                                <div class="col-lg-6">
                                    <input type="text" name="usercontact" class="form-control" autocomplete="off" placeholder="Enter Contact Number" value="{{$update->contact}}">
                                    @error('usercontact')
                                        <div class="alert alert-danger">{{$message}}</div>
                                    @enderror
                                </div>
                                <div class="col-lg-6">
                                    <input type="file" name="userprofile"  class="form-control" autocomplete="off" value="{{$update->profile}}">
                                    @error('userprofile')
                                        <div class="alert alert-danger">{{$message}}</div>
                                    @enderror
                                </div>
                            </div>
                            {{-- row-3 --}}
                            <div class="row m-2">
                                <div class="col-lg-6">
                                    <button type="submit" class="submitbutton">SUBMIT</button>
                                </div>
                                <div class="col-lg-6">
                                    <img src="{{asset('crud_asset/image/'). '/' .$update->profile}}" alt="" style="height:100px;" class=" mt-2 img-fluid img-thumbnail">
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

The provided Laravel Blade template extends a layout named 'crud.layout' and defines the content section. The page displays a form for updating user information in a CRUD operation. It includes fields for the user's name, email, contact number, and a file input for the user's profile picture. The form is populated with the existing data for the user, retrieved from the controller. Validation error messages are displayed for each input field if there are any. Upon form submission, the data is sent to the 'updatesubmit.data' route, passing the user's ID for identification. Success or error messages are shown based on the outcome, and the user's existing profile picture is displayed with an option to upload a new one. The layout provides a consistent structure for the page, including a card layout with a title, and alerts for success or error messages.

Laravel-Crud-Operation

UPDATE FORM



Note:- I have not explained the password update process. I will explain the password update process upcoming article.

Edit Form View

public function updateview($id)
{
    $update = CrudModel::find($id);
    return view('crud.pages.edit', compact('update'));
}

The given PHP code defines a method named `updateview` within a Laravel controller. This method is designed for handling the display of an edit form in a CRUD (Create, Read, Update, Delete) operation. It takes a parameter `$id`, presumably representing the unique identifier of the data record to be edited. Using Laravel's Eloquent ORM, the code retrieves the corresponding record from the database using the `find` method on the `CrudModel`. The retrieved record is then passed to a Blade view named 'crud.pages.edit' using the `compact` function, allowing the view to access and display the data in an edit form. In essence, this method is responsible for fetching the data to be edited and rendering the corresponding view for users to update the information associated with the specified record.

Update Code

public function updatedate(Request $request, $id)
{
    $edit_data = CrudModel::find($id);
    if($request->file('userprofile'))
    {
        $imagename = $request->file('userprofile');
        $imagerename = $request->input('username') . '_' . time() . '.' . $imagename->getClientOriginalExtension();
        $located = public_path('/crud_asset/image/');
        $edit_data->name = $request->input('username');
        $edit_data->email = $request->input('useremail');
        $edit_data->contact = $request->input('usercontact');
        $edit_data->profile = $imagerename;
        $edit_data->update();
        if($edit_data)
        {
            $imagename->move($located, $imagerename );
            return redirect()->route('view.data')->with('success', 'Record has been update successfully');
        }
        else
        {
            return redirect()->back()->with('error', 'Record has not been update successfully');
        }
    }
    else
    {
        $edit_data->name = $request->input('username');
        $edit_data->email = $request->input('useremail');
        $edit_data->contact = $request->input('usercontact');
        $edit_data->update();
        if($edit_data)
        {
            return redirect()->route('view.data')->with('success', 'Record Has Been Successfully Update');
        }
        else
        {
            return redirect()->back()->with('error', 'Record Has Not Been Successfully Update');
        }
    }
}

The provided PHP code is a method named `updatedate` within a Laravel controller, likely part of a CRUD (CreateReadUpdateDelete) functionality. This method is responsible for updating a data record based on the provided `$id` and the form data submitted through a POST request. The method first retrieves the existing data record using Eloquent's `find` method, and then it checks whether a new profile picture has been uploaded. If a new profile picture is present, it processes the image, updates the record with the submitted form data, and moves the uploaded image file to the specified directory. If the update is successful, it redirects to the 'view.data' route with a success message; otherwise, it redirects back with an error message. If no new profile picture is uploaded, the method simply updates the record with the submitted form data and follows the same logic for success or error redirection.

Post a Comment

Previous Post Next Post

Recent in Technology