Update data from database using Laravel framework
In this example, we will discuss how to update a record or data from the MySQL database using Laravel framework PHP.
Note: The WHERE clause specifies which data should be updated. If you omit the WHERE clause, all records or data will be updated!
In the below example we update the employee data from MySQL database using JSP.
Create 4 files for update data
1. Install Laravel Fresh Setup
First of all, we need to download the laravel fresh setup. Use the below command and download fresh new laravel setup :
composer create-project --prefer-dist laravel/laravel Blog
2. Setup Database
After successfully install laravel Application, Go to your project .env file and set up database credential and move next step :
UPDATE table_nameSET column1=value, column2=value2,...WHERE some_column=some_valueCREATE TABLE `tbl_insert` ( `id` int(11) NOT NULL, `first_name` varchar(50) DEFAULT NULL, `last_name` varchar(50) DEFAULT NULL, `city_name` varchar(50) DEFAULT NULL, `email` varchar(50) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
3. Make Migration file with Model
- StudUpdateController.php (app/Http/Controllers/StudUpdateController.php)
- stud_edit_view.blade.php (resources/views/stud_edit_view.blade.php)
- stud_update.php (resources/views/stud_update.php)
- web.php (routes/web.php)
The student's table looks like before the update.
id | first name | last name | City name | Email Id | Action |
---|---|---|---|---|---|
1 | Soeng | Sony | PP | divyasundar@gmail.com | Edit |
2 | Chan | Do | USA | hritika@gmail.com | Edit |
3 | Na | Ry | PP | milanjena@gmail.com | Edit |
Now I am going to update the email id of id=3 record.
Step 2: Create Blade File
In Last step, let's create import.blade.php(resources/views/import.blade.php) for layout and we will write design code here and put the following code:
resources/views/stud_edit_view.blade.php
stud_edit_view.blade.php
<!Doctype html>
<html>
<head>
<title>View Student Records</title>
<!-- library -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap4.min.css">
<!-- library bootstrap -->
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap4.min.js"></script>
</head>
<body>
<br>
<br>
<div class="container">
@if(Session::has('message'))
<div class="alert alert-success" id="res_message">
{{ Session::get('message') }}
</div>
@endif
<script>
$(document).ready(function(){
setTimeout(function() {
$('#res_message').hide();
},3000);
});
</script>
<table id="tableHorizontalWrapper" class="table table-striped table-bordered table-sm text-center" cellspacing="0"width="50%">
<tr>
<td>ID</td>
<td>First Name</td>
<td>Lastst Name</td>
<td>City Name</td>
<td>Email</td>
<td>Edit</td>
</tr>
@foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->first_name }}</td>
<td>{{ $user->last_name }}</td>
<td>{{ $user->city_name }}</td>
<td>{{ $user->email }}</td>
<td><a href = 'edit/{{ $user->id }}'>Edit</a></td>
</tr>
@endforeach
</table>
</div>
</body>
</html> |
stud_update.php
<!DOCTYPE html>
<html>
<head>
<title>Student Management | Edit</title>
<!-- library -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap4.min.css">
<!-- library bootstrap -->
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap4.min.js"></script>
</head>
<body>
<div class="container">
<form action = "/edit/<?php echo $users[0]->id; ?>" method = "post">
<input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
<table>
<tr>
<td>First Name</td>
<td>
<input type = 'text' class="form-control input-sm" name = 'first_name'value = '<?php echo$users[0]->first_name; ?>'/> </td>
</tr>
<tr>
<td>Last Name</td>
<td>
<input type = 'text'class="form-control input-sm" name = 'last_name'value = '<?php echo$users[0]->last_name; ?>'/>
</td>
</tr>
<tr>
<td>City Name</td>
<td>
<input type = 'text'class="form-control input-sm" name = 'city_name'value = '<?php echo$users[0]->city_name; ?>'/>
</td>
</tr>
<tr>
<td>Email</td>
<td>
<input type = 'text'class="form-control input-sm" name = 'email'value = '<?php echo$users[0]->email; ?>'/>
</td>
</tr>
<tr>
<br>
<td colspan = '2'>
<input type = 'submit'class="btn btn-danger" value = "Update student" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
|
Step 3: Create Controller
In this step, now we should create a new controller as MyController in this path "app/Http/Controllers/MyController.php". this controller will manage all importExportView, export and import request and return a response, so put bellow content in the controller file:
app/Http/Controllers/StudUpdateController
StudUpdateController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class StudUpdateController extends Controller
{
public function index()
{
$users = DB::select('SELECT * FROM tbl_insert');
return view('stud_edit_view',['users'=>$users]);
}
public function show($id)
{
$users = DB::select('SELECT * FROM tbl_insert WHERE id = ?',[$id]);
return view('stud_update',['users'=>$users]);
}
public function edit(Request $request,$id)
{
$first_name = $request->input('first_name');
$last_name = $request->input('last_name');
$city_name = $request->input('city_name');
$email = $request->input('email');
DB::update('UPDATE tbl_insert SET first_name = ?,last_name=?,city_name=?,email=? WHERE id = ?',[$first_name,$last_name,$city_name,$email,$id]);
return redirect('edit-records')->with('message' ,'Record updated successfully.');
}
} |
routes/web.php
web.php
php
/* |--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('edit-records','StudUpdateController@index');
Route::get('edit/{id}','StudUpdateController@show');
Route::post('edit/{id}','StudUpdateController@edit'); |
PHP Run:
php artisan serve
http://127.0.0.1:8000/edit-records
0 Comments
CAN FEEDBACK
Emoji