For getting every new tutorial link Please join our telegram group

Laravel delete() vs truncate() vs drop() method query builder tutorial

In this tutorial we differentiate laravel three data delete methods. difference between delete vs truncate vs drop method.

Laravel query builder methods for deleting table data

  1. delete()
  2. truncate()
  3. drop()

Laravel delete() method

laravel delete() method is used for delete a database table rows. this method most commnly used for delete records.This method is call after getting a single row record for delete that row data.see the below example id = 7 data available we get that data and apply delete() method.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class EmployeeController extends Controller{
 public function delete(){
    $id = 7;
    $employee = DB::table('employees')->where('id', $id)->delete();

     return 'data successfully deleted';
  }

}

Laravel truncate() method

truncate method also used for deleting table records but it delete or reset full table thus truncate() method used when require to deleting full table data.after run truncate method data store again by id=1.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class EmployeeController extends Controller{
    public function truncate(){
       
       $employee = DB::table('employees')->truncate();

       return 'table successfully truncated';
  }

}

Laravel drop() method

one of the last method is used for delete drop() method it delete table also from database.this method normally not used because in many cases it delete directly by database and we also delete our table by migration.

run this migration for make delete migration

php artisan make:migration drop_employees_table

this command create a migration for employees table and we change up method by this.

public function up() {
        Schema::dropIfExists('employees');
    }

laravel delete() vs truncate() method

laravel use delete and truncate method for delete table data the main difference between delete vs truncate is delete method delete rows one by one where as truncate method delete all table data.if we delete all data by table than new id start with last updated data and in truncate's case id start by one.

laravel delete() vs drop() vs truncate() method

delete and truncate method use for delete a row and all table and drop method is delete table from database.

You read this tutorial on advanced web tuts. watch all query builder playlist click on sidebar playlist link

In this tutorial we differentiate laravel three data delete methods. difference between delete vs truncate vs drop method.

Laravel query builder methods for deleting table data

  1. delete()
  2. truncate()
  3. drop()

Laravel delete() method

laravel delete() method is used for delete a database table rows. this method most commnly used for delete records.This method is call after getting a single row record for delete that row data.see the below example id = 7 data available we get that data and apply delete() method.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class EmployeeController extends Controller{
 public function delete(){
    $id = 7;
    $employee = DB::table('employees')->where('id', $id)->delete();

     return 'data successfully deleted';
  }

}

Laravel truncate() method

truncate method also used for deleting table records but it delete or reset full table thus truncate() method used when require to deleting full table data.after run truncate method data store again by id=1.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class EmployeeController extends Controller{
    public function truncate(){
       
       $employee = DB::table('employees')->truncate();

       return 'table successfully truncated';
  }

}

Laravel drop() method

one of the last method is used for delete drop() method it delete table also from database.this method normally not used because in many cases it delete directly by database and we also delete our table by migration.

run this migration for make delete migration

php artisan make:migration drop_employees_table

this command create a migration for employees table and we change up method by this.

public function up() {
        Schema::dropIfExists('employees');
    }

laravel delete() vs truncate() method

laravel use delete and truncate method for delete table data the main difference between delete vs truncate is delete method delete rows one by one where as truncate method delete all table data.if we delete all data by table than new id start with last updated data and in truncate's case id start by one.

laravel delete() vs drop() vs truncate() method

delete and truncate method use for delete a row and all table and drop method is delete table from database.

You read this tutorial on advanced web tuts. watch all query builder playlist click on sidebar playlist link

php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Related tutorial links