How to delete data from database using Laravel 7 framework
In this example, we will discuss how to delete a record or data from the MySQL database using laravel framework PHP.
The DELETE statement is used to delete records from a table:
DELETE FROM table_name
WHERE some_column = some_value
CREATE 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;
|
Notice: The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!
To learn more about SQL, please visit our SQL tutorial.
Create 3 files for delete data
- StudDeleteController.php (app/Http/Controllers/StudDeleteController.php)
- stud_delete_view.blade.php (resources/views/stud_delete_view.blade.php)
- web.php (routes/web.php)
id | first name | last name | City name | Email Id | Action |
---|---|---|---|---|---|
1 | Fo | Go | PP | divyasundar@gmail.com | Delete |
2 | Lo | Ky | PP | hritika@gmail.com | Delete |
3 | Foley | Jena | USA | milanjena@gmail.com | Delete |
Now I am going to delete the id=3 record.
For delete record, we use 3 files here
stud_delete_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('delete'))
<div class="alert-success text-center" id="res_message">
{{ Session::get('delete') }}
</div>
@endif
<script>
$(document).ready(function(){
setTimeout(function() {
$('#res_message').hide();
},3000);
});
</script>
@if(Session::has('message'))
<div class="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>
<td>Delete</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>
<td><a href = 'delete/{{ $user->id }}'>Delete</a></td>
</tr>
@endforeach
</table>
</div>
</body>
</html> |
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-success text-center" id="res_message">
{{ Session::get('message') }}
</div>
@endif
<script>
$(document).ready(function(){
setTimeout(function() {
$('#res_message').hide();
},3000);
});
</script>
@if(Session::has('delete'))
<div class="alert-success text-center" id="res_message">
{{ Session::get('delete') }}
</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>
<td>Delete</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>
<td><a onClick="return confirm('Are you sure you want to delete?')" href = 'delete/{{ $user->id }}'>Delete</a></td>
</tr>
@endforeach
</table>
</div>
</body>
</html> |
views.blade.php
|
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> |
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-success text-center" id="res_message">
{{ Session::get('message') }}
</div>
@endif
<script>
$(document).ready(function(){
setTimeout(function() {
$('#res_message').hide();
},3000);
});
</script>
@if(Session::has('delete'))
<div class="alert-success text-center" id="res_message">
{{ Session::get('delete') }}
</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>
<td>Delete</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>
<td><a onClick="return confirm('Are you sure you want to delete?')" href = 'delete/{{ $user->id }}'>Delete</a></td>
</tr>
@endforeach
</table>
</div>
</body>
</html> |
stud_create.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Student Management | Add</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<br>
<h2 class="text-center">Form Insert Data Student</h2>
@if(Session::has('insert'))
<div class="alert-success text-center" id="res_message">
{{ Session::get('insert') }}
</div>
@endif
<script>
$(document).ready(function(){
setTimeout(function() {
$('#res_message').hide();
},3000);
});
</script>
<form action = "/create" method = "post">
<input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
<div class="form-group">
<div class="row">
<div class="col-6">
<label for="usr">First Name:</label>
<input type='text'class="form-control" name='first_name' required >
</div>
<div class="col-6">
<label for="usr">Last Name:</label>
<input type="text" class="form-control" name='last_name' required>
</div>
</div>
</div>
<div class="form-group">
<label for="pwd">City Name:</label>
<select class="form-control" name="city_name" required>
<option >Please select city</option>
<option value="Phnom Penh">Phnom Penh</option>
<option value="USA">USA</option>
<option value="English">English</option>
<option value="Canada">Canada</option>
<option value="Frence">Frence</option>
</select>
</div>
<div class="form-group">
<label for="pwd">City Name:</label>
<input type="text" class="form-control" name='email' required>
</div>
<input type ='submit' class=" btn btn-success text-center" value = "Insert">
</div>
</form>
</body>
</html> |
StudInsertController.php
|
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.');
}
}
|
StudDeleteController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class StudDeleteController extends Controller
{
public function index()
{
$users = DB::select('SELECT * FROM tbl_insert');
return view('stud_delete_view',['users'=>$users]);
}
public function destroy($id)
{
DB::delete('DELETE FROM tbl_insert WHERE id = ?',[$id]);
return redirect('edit-records')->with('delete' ,'Record deleted successfully.');
}
} |
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 for insert data
Route::get('insert','StudInsertController@insertform');
Route::post('create','StudInsertController@insert');
Route::get('view','ViewController@view');
//View Page
Route::get('ViewPages', 'ViewController@index');
Route::post('ViewPages', 'ViewController@index');
Route::get('ajax-form-submit', 'FormController@index');
Route::post('ajax-form-submit', 'FormController@index');
Route::post('save-form', 'FormController@store');
//edite
Route::get('edit-records','StudUpdateController@index');
Route::get('edit/{id}','StudUpdateController@show');
Route::post('edit/{id}','StudUpdateController@edit');
//delete data
Route::get('delete-records','StudDeleteController@index');
Route::get('delete/{id}','StudDeleteController@destroy'); |
0 Comments
CAN FEEDBACK
Emoji