CodeIgniter Laravel PHP Example Javascript jQuery MORE
Need a Website Or Web Application Or Any Help.Contact Us: +91 9437911966 (Whatsapp) Note: Paid Service

How to delete data from database using Laravel framework


In this example we will discuss about how to delete a record or data from 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

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 file 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)

The following examples delete the record with id=3 in the "users" table:

To delete a record from table you must have insert data.If you want to know how to insert data in laravel framework please visit : Insert data in Laravel.

The students table look like this:

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

Now i am going to delete the id=3 record.

For delete record we use 3 file here

stud_delete_view.blade.php

<!DOCTPE html>
<html>
<head>
<title>View Student Records</title>
</head>
<body>
<table border = "1">
<tr>
<td>ID</td>
<td>First Name</td>
<td>Last 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 = 'delete/{{ $user->id }}'>Delete</a></td>
</tr>
@endforeach
</table>
</body>
</html>

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 student_details');
return view('stud_delete_view',['users'=>$users]);
}
public function destroy($id) {
DB::delete('delete from student_details where id = ?',[$id]);
echo "Record deleted 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!
|
*/
//delete data
Route::get('delete-records','StudDeleteController@index');
Route::get('delete/{id}','StudDeleteController@destroy');

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 Delete
2 Hritika Sahu Pune hritika@gmail.com Delete