To Delete data from a table we use SQL Delete statement
DELETE FROM table_name WHERE condition;
To learn more about SQL, please visit our BajarangiSoft site.
How to delete data in database using laravel controller
Let's see
1.Create table in database.
The query builder may also be used to delete records from the table through the delete
method.
DB::table('users')->delete();//delete all data
You may constrain delete
statements by adding where
clauses before calling the delete
method:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;//include this
class UserController extends Controller
{
public function update($id)
{
$user = DB::table('users')->where('id', '=',$id )->delete();//delete particular id data
return 'Data deleted successfully';
}
}
?>
If you wish to truncate the entire table, which will remove all rows and reset the auto-incrementing ID to zero, you may use the truncate
method:
DB::table('users')->truncate();