Laravel | Delete Records Last Updated : 27 Apr, 2020 Comments Improve Suggest changes Like Article Like Report To delete records we can use DB facade with the delete method. To do so follow the below steps one by one: Step 1: Create Controller UserController by executing this command. php artisan make:controller UserController Step 2: We can delete records in two ways. First Method: The first is to delete direct using database command. Write following Code in App/Http/Controllers/UserController.php PHP <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use DB; class UserController extends Controller { public function index() { $users = DB::select('SELECT * FROM users'); return view('user', ['users'=>$users]); } public function destroy($id) { DB::delete('DELETE FROM users WHERE id = ?', [$id]); echo ("User Record deleted successfully."); return redirect()->route('users.index'); } } Second Method: The second way is to delete using the Laravel delete Function and User Model (Easy one). PHP <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\User; class UserController extends Controller { public function index() { $users = User::All(); return view('user', ['users'=>$users]); } public function destroy($id) { $user = User::where('id', $id)->firstorfail()->delete(); echo ("User Record deleted successfully."); return redirect()->route('users.index'); } } Step 3: Implementation or Driver Code and create web routes for implementation of the above code in routes/web.php PHP <?php Route::get('/user', 'UserController@index')->name('users.index'); Route::delete('/user/{id}', 'UserController@destroy') ->name('users.destroy'); ?> Step 4: Create a View File from where we display our users in resources/views directory name user.blade.php. Write following HTML code. HTML <!DOCTYPE html> <html> <head> <title>Users Record</title> <style type="text/css"> table { color: #333; font-family: sans-serif; width: 640px; border-collapse: collapse; border-spacing: 0; } td, th { border: 1px solid #CCC; height: 30px; } th { background: #F3F3F3; font-weight: bold; } td { background: #FAFAFA; text-align: center; } </style> </head> <body> <table> <tr> <td>ID</td> <td>Name</td> <td>Email</td> <td>Delete</td> </tr> @foreach ($users as $user) <tr> <td>{{ $user->id }}</td> <td>{{ $user->name }}</td> <td>{{ $user->email }}</td> <td><a href="{{ route('users.index') }}" onclick="event.preventDefault(); document.getElementById( 'delete-form-{{$user->id}}').submit();"> Delete </a> </td> <form id="delete-form-{{$user->id}}" + action="{{route('users.destroy', $user->id)}}" method="post"> @csrf @method('DELETE') </form> </tr> @endforeach </table> </body> </html> Step 5: Start the server by executing php artisan:serve command and go to http://localhost:8000/user and the output will be: Output: Click on the delete button to get the record deleted. After deleting two records output is: Comment More infoAdvertise with us Next Article Laravel | Delete Records shivamsinghal1012 Follow Improve Article Tags : Web Technologies PHP Laravel Similar Reads Laravel | Directory Structure When you will create your fresh Laravel application, it will contain a large number of folders as shown in image below: Each of these folders fulfills a specific task for the overall functioning of the framework. The purpose of each of these folders is explained below but before that let's look at e 4 min read WordPress Delete Tags Tags in WordPress help organize content and improve site navigation, but sometimes, tags need to be deleted. Whether you're cleaning up your site or correcting mistakes, deleting tags is a straightforward process. In this guide, we'll walk you through how to delete tags in WordPress, step-by-step.Wh 3 min read Laravel | Eloquent Model Basics Laravel is an MVC based PHP framework. In MVC architecture, âMâ stands for âModelâ. A Model is basically a way for querying data to and from the table in the database. Laravel provides a simple way to do that using Eloquent ORM (Object-Relational Mapping). Every table has a Model to interact with th 5 min read WordPress Delete Media WordPress is a free Content Management System (CMS) built using PHP and MySQL. Matt Mullenweg is the developer behind WordPress. It's widely used for creating dynamic websites, offering users a platform to customize and manage their content efficiently from the back-end. WordPress streamlines the pr 4 min read WordPress Delete Links WordPress is a content management system (CMS) that allows users to create and manage websites with ease. Whether you're a blogger or a developer, knowing how to manage links on your WordPress site is important. In this article, we'll walk you through the process of deleting links in WordPress, maki 3 min read WordPress Delete Users WordPress is a robust platform. Whether you manage a small blog or a large e-commerce site, handling user accounts efficiently is important. One of the essential aspects of user management is knowing how to delete users in WordPress. This guide will deal with the details of deleting users in WordPre 3 min read Laravel | CSRF Protection Cross-Site Request Forgery (CSRF) is a type of attack that performed by the attacker to send requests to a system with the help of an authorized user who is trusted by the system. Laravel provides protection with the CSRF attacks by generating a CSRF token. This CSRF token is generated automatically 3 min read Laravel Features Laravel is an open-source web framework written in PHP that follows the modelâviewâcontroller (MVC) architectural pattern. It is intended for the development of web applications following the principles of modelâviewâcontroller (MVC) architecture. Laravel is free and open-source and released under t 2 min read WordPress Delete Posts WordPress is one of the most popular content management systems (CMS) in the world, making it easy for anyone to create and manage their own website. As you publish content, you might find that some posts become outdated or no longer relevant. In such cases, deleting posts is a necessary task to kee 3 min read Laravel | Front-end Scaffolding Front-end Scaffolding means to create a basic structure for an application. Laravel provides a very simple way to change the front-end presets/scaffolding with any of the other available scaffolding like Bootstrap, Vue and React. Generate Scaffolding: Step 1: To generate a scaffolding, we first need 2 min read Like