CodeIgniter Laravel PHP Example HTML Javascript jQuery MORE Videos New

Update data from database using Laravel framework


In this example we will discuss about how to update a record or data from MySQL database using laravel framework PHP.

To update the data in mysql table UPDATE statement is used.

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value 

Note: The WHERE clause specifies which data that 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 file for update data

  • 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 students table look like before update.

id first name last name City name Email Id Action
1 Divyasundar Sahu Mumbai divyasundar@gmail.com Edit
2 Hritika Sahu Pune hritika@gmail.com Edit
3 Milan Jena Chennai milanjena@gmail.com Edit

Now i am going to update the email id of id=3 record.

stud_edit_view.blade.php

<!Doctype html>
<html>
<head>
<title>View Student Records</title>
</head>
<body>
<table border = "1">
<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>
</body>
</html>

stud_update.php

<!DOCTYPE html>
<html>
<head>
<title>Student Management | Edit</title>
</head>
<body>
<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' name = 'first_name'
value = '<?php echo$users[0]->first_name; ?>'/> </td>
</tr>
<tr>
<td>Last Name</td>
<td>
<input type = 'text' name = 'last_name'
value = '<?php echo$users[0]->last_name; ?>'/>
</td>
</tr>
<tr>
<td>City Name</td>
<td>
<input type = 'text' name = 'city_name'
value = '<?php echo$users[0]->city_name; ?>'/>
</td>
</tr>
<tr>
<td>Email</td>
<td>
<input type = 'text' name = 'email'
value = '<?php echo$users[0]->email; ?>'/>
</td>
</tr>
<tr>
<td colspan = '2'>
<input type = 'submit' value = "Update student" />
</td>
</tr>
</table>
</form>
</body>
</html>

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 student');
return view('stud_edit_view',['users'=>$users]);
}
public function show($id) {
$users = DB::select('select * from student 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');
/*$data=array('first_name'=>$first_name,"last_name"=>$last_name,"city_name"=>$city_name,"email"=>$email);*/
/*DB::table('student')->update($data);*/
/* DB::table('student')->whereIn('id', $id)->update($request->all());*/
DB::update('update student set first_name = ?,last_name=?,city_name=?,email=? where id = ?',[$first_name,$last_name,$city_name,$email,$id]);
echo "Record updated successfully.
";
echo 'Click Here to go back.';
}
}

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');

After delete the record the table look like this.

id first name last name City name Email Id Action
1 Divyasundar Sahu Mumbai divyasundar@gmail.com Edit
2 Hritika Sahu Pune hritika@gmail.com Edit
3 Milan Jena Mumbai milan@gmail.com Edit