In this tutorial, we will explore how to update data in a Laravel application. Laravel provides a convenient and expressive way to interact with databases, making the process of updating records seamless.
before update data from database using laravel you need to know how to insert data into database and how to display data from database in laravel . To know more click on the link given below
for insert :
https://codingstatus.com/insert-form-data-into-database-using-laravel/
for Display:
https://codingstatus.com/display-data-from-database-in-table-using-laravel/
Update Data from Database in table in Laravel 10
After insert and fetching the data from the database in the table we will create the function for edit and update data .
1.Display data from database in table .
views file : home.blade.php
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel CRUD Operation</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous"> </head> <body> <div class="container mt-5"> <div class="row"> <div class="col-sm-6"> <table class="table table-hover"> <thead> <tr> <th scope="col">ID</th> <th scope="col">Name</th> <th scope="col">City</th> <th scope="col">Marks</th> <th scope="col">Actions</th> </tr> </thead> <tbody> @foreach ($students as $stu) <tr> <th>{{$stu->id}}</th> <td>{{$stu->name}}</td> <td>{{$stu->city}}</td> <td>{{$stu->marks}}</td> <td> <a href="" class="btn btn-success btn-sm">Edit</a> <a href="" class="btn btn-danger btn-sm">Delete</a> </td> </tr> @endforeach </tbody> </table> </div> </div> </div> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script> </body> </html>
2.Create Edit button
After display all the records. Now we create an edit button for each record in the HTML table.
just add the following line in the home.blade.php
view file :home.blade.php
<a href="{{ url('/edit', $stu->id) }}" class="btn btn-success btn-sm">Edit</a>
{{ url(‘/edit’, $stu->id) }}: This Blade syntax generates the URL for the “Edit” link. It uses the url helper function to generate a URL based on the given parameters. In this case, it creates a URL with the path ‘/edit’ and appends the $stu->id as a parameter, resulting in something like “/edit/1” where “1” is the ID of the student.
3. create a routes
route file : web.php
first of all import the StudentController in routes file like:
use App\Http\Controllers\StudentController;
now create a route url for edit and Update
Route::get('/edit/{id}',[StudentController::class,'edit'])->name('edit'); Route::put('/edit/{id}',[StudentController::class,'update'])->name('update');
The first(edit) route handles the display of the edit form for a student with a specific ID.The edit
route is a GET request
the second route processes the form submission to update the corresponding student record and the update
route is a PUT request
both directed to the edit
and update
methods in the StudentController
respectively, with the route names set as ‘edit’ and ‘update’ for ease of reference in the application.
4.Create a Controller
Controller file: StudentController.php
After create the routes now we create the controller for edit and update method in student controller
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller { public function edit(string $id) { $student = Student::find($id); return view('editForm',['student'=>$student]); } }
In StudentController class, there is a public method named edit
. This method takes a single parameter, a string named $id
. The $id
parameter is likely used to identify the specific student record to be edited.and then renders a view named ‘editForm’, passing the student data to the view for display and possible editing.
For update data in StudentController file add the following line to update data
public function update(Request $request, string $id) { $student = Student::find($id); $student->name=$request->name; $student->city=$request->city; $student->marks=$request->marks; $student->save(); return redirect(route('index'))->with('status', 'student Updated !'); }