Update data into database in laravel 8
In this tutorial, you will be learning how to edit and update data from database using Eloquent Model in Laravel 8.
So guys, to edit and update data in laravel, you have to create a laravel project, connect your database in .env file, then you need to create you Model, Controller and Migration to insert & fetch data in laravel.
Step 1: Fetch data from database in index.blade.php file in the following path: resources/views/student/index.blade.php
Let fetch the data in HTML Table to make edit button in file as follows:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4>How to Edit and Update Data in Laravel
<a href="{{ url('add-student') }}" class="btn btn-primary float-end">Add Student</a>
</h4>
</div>
<div class="card-body">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Course</th>
<th>Section</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
@foreach ($student as $item)
<tr>
<td>{{ $item->id }}</td>
<td>{{ $item->name }}</td>
<td>{{ $item->email }}</td>
<td>{{ $item->course }}</td>
<td>{{ $item->course }}</td>
<td>
<a href="{{ url('edit-student/'.$item->id) }}" class="btn btn-primary btn-sm">Edit</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
@endsection
Step 2: Create a edit.blade.php file in the following path: resources/views/student/edit.blade.php to edit and update data as follows:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
@if (session('status'))
<h6 class="alert alert-success">{{ session('status') }}</h6>
@endif
<div class="card">
<div class="card-header">
<h4>Edit & Update Student
<a href="{{ url('students') }}" class="btn btn-danger float-end">BACK</a>
</h4>
</div>
<div class="card-body">
<form action="{{ url('update-student/'.$student->id) }}" method="POST">
@csrf
@method('PUT')
<div class="form-group mb-3">
<label for="">Student Name</label>
<input type="text" name="name" value="{{$student->name}}" class="form-control">
</div>
<div class="form-group mb-3">
<label for="">Student Email</label>
<input type="text" name="email" value="{{$student->email}}" class="form-control">
</div>
<div class="form-group mb-3">
<label for="">Student Course</label>
<input type="text" name="course" value="{{$student->course}}" class="form-control">
</div>
<div class="form-group mb-3">
<label for="">Student Section</label>
<input type="text" name="section" value="{{$student->section}}" class="form-control">
</div>
<div class="form-group mb-3">
<button type="submit" class="btn btn-primary">Update Student</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
Step 3: Give a route to call the function in your controller for calling the page in the following path: routes/web.php as follows:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\StudentController;
Route::get('students', [StudentController::class, 'index']);
Route::get('edit-student/{id}', [StudentController::class, 'edit']);
Route::put('update-student/{id}', [StudentController::class, 'update']);
Step 4: Let's go to the controller in the following path : app/Http/Controllers/StudentController.php file.
<?php
namespace App\Http\Controllers;
use App\Models\Student;
use Illuminate\Http\Request;
class StudentController extends Controller
{
public function index()
{
$student = Student::all();
return view('student.index', compact('student'));
}
public function edit($id)
{
$student = Student::find($id);
return view('student.edit', compact('student'));
}
public function update(Request $request, $id)
{
$student = Student::find($id);
$student->name = $request->input('name');
$student->email = $request->input('email');
$student->course = $request->input('course');
$student->section = $request->input('section');
$student->update();
return redirect()->back()->with('status','Student Updated Successfully');
}
}
Step 5: We are successfully done with Edit and Update Coding, lets test it guys by serving this application as following command:
$ php artisan serve
now lets go to the browser at the link: http://localhost:8000/students
Thanks for reading.