
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Delete a Single Record in Laravel 5
There are different ways you can do it. Let us test a few examples that help us delete a single record in Laravel.
Assume we have created a table named users table as shown below ?
mysql> select * from users; +----+---------------+------------------+--------------+------------+------+ | id | name | email | password | address | age | +----+---------------+------------------+--------------+------------+------+ | 1 | Siya Khan | [email protected] | hgfdv3vhjd | Xyz | 20 | | 2 | Rehan Khan | [email protected] | cskjg367dv | Xyz | 18 | | 3 | Rehan Khan | [email protected] | dshjgcv2373h | testing | 20 | | 4 | Rehan | [email protected] | abcd | vci4hn4f4 | 15 | | 5 | Nidhi Agarwal | [email protected] | fjh99302v | abcd | 20 | | 6 | Ashvik Khanna | [email protected] | 83bg44f4f | oooo | 16 | | 7 | Viraj Desai | [email protected] | ppsdkj39hgb2 | test | 18 | | 8 | Priya Singh | [email protected] | chjbbvgy3ij | test123 | 20 | +----+---------------+------------------+--------------+------------+------+
Example 1
You can use find() method accepts a key value as a parameter and returns the model that has the given key as the primary key. The delete() method removes the current record. Following example demonstrates how to remove a record using these two methods
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller { public function index() { $userDetails = User::find(9); $deleteuser = $userDetails->delete(); if ($deleteuser) { echo "Record deleted from table successfully"; } else { echo "Error in Deletion"; } } }
Output
The output of the above code is as follows
Record deleted from table successfully
Example 2
You can add condition in where clause to delete the record from table as shown below ?
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller{ public function index() { $deleteuser = User::where('id', '=', 11)->delete(); if ($deleteuser) { echo "Record deleted from table successfully"; } else { echo "Error in Deletion"; } } }
Output
The output of the above code is ?
Record deleted from table successfully
Example 3
You can also make use of the destroy() method to delete records from the table.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller { public function index() { $deleteuser = User::destroy(12);; if ($deleteuser) { echo "Record deleted from table successfully"; } else { echo "Error in Deletion"; } } }
Output
The output of the above code is ?
Record deleted from table successfully
Example 4
If you want to delete multiple records at a time you can do so as shown below ?
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller{ public function index() { $deleteuser = User::destroy([15,16,17]); if ($deleteuser) { echo "Record deleted from table successfully"; } else { echo "Error in Deletion"; } } }
Output
The output of the above code is ?
Record deleted from table successfully