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
- delete()
- truncate()
- 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
- delete()
- truncate()
- 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
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
- laravel query builder introduction tutorial
- part 1 laravel query insert data
- part 2 laravel all type of where conditions in query builder
- part 3 laravel whereDate, whereYear and whereTime qyery method
- part 4 compare two db columns in laravel query builder
- part 6 laravel pluck method get single column value
- part 7 laravel select and addselect query in query builder
- part 5 laravel first and find queries
- part 8 laravel edit and update method query explaination
- Part 9 laravel query for update or insert data by one method
- part 10 laravel delete vs truncate vs drop method
- part 11 gropupby and orderby query tutorial
- Part 12 laravel union of two queries
- part 13 laravel all aggregate functions with example
- part 14 laravel query builder chunk method
- part 15 laravel exists() and doesntExist() method
- part 16 laravel paginate vs simplePaginate method
- part 17 laravel all join methods