How To Delete Data To Database With Example In Laravel

admin_img Posted By Bajarangi soft , Posted On 23-09-2020

Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works on all supported database systems. The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks. There is no need to clean strings being passed as bindings.

How to Delete Data from DataBase In Laravel

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

Related Post