CRUD operations in Laravel are quite simple and efficient. In this article, we will specifically focus on the delete operation. Delete means removing a record from your database permanently. You will see how you can delete a specific record in Laravel,
You must have learned about CRUD in which the delete operation plays a very important role when you need to remove unwanted data stored in your database. With the help of Laravel’s powerful ORM (Eloquent) and built-in functions, the delete operation becomes very easy. Today we will learn step-by-step how to delete a record and how to handle common errors in our process.
Steps of the Delete Operation:
Step 1: When creating a route in Laravel, we usually pass a parameter in the URL. In this case, we will pass a unique identifier that will uniquely identify any record. In Laravel, we include parameters in the URL path.
When you create a route to delete a record, you will need a parameter (for example, id) that will uniquely identify which record should be deleted. For this, you need to define a route.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\crud\CrudController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
// Route::get('/', function () {
// return view('welcome');
// });
Route::get('/', [CrudController::class, 'index'])->name('form');
Route::post('/submit_data', [CrudController::class, 'submit'])->name('submit.form');
Route::get('/crud/data', [CrudController::class, 'showdata'])->name('view.data');
Route::get('/updateform/{id}', [CrudController::class, 'updateview'])->name('updateview.data');
Route::post('/updatesubmit/{id}', [CrudController::class, 'updatedate'])->name('updatesubmit.data');
Route::get('/delete/{id}', [CrudController::class, 'deleterecord'])->name('delete.data');
Step 2: In Laravel we find the record through the model. First, you need to find the record that you want to delete.
<?php
namespace App\Http\Controllers\Crud;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\crud\CrudModel;
use Hash;
class CrudController extends Controller
{
public function deleterecord(request $request, $id)
{
$delete_data = CrudModel::find($id);
$delete_data->delete();
if($delete_data)
{
return redirect()->back()->with('success', 'Record Has Been Successfully Delete.');
}
else
{
return redirect()->back()->with('error', 'Record Has Not Been Successfully Delete');
}
}
}
The purpose of this code is to delete a record which is a CRUD (create, read, update, delete) operation. This code is written in the PHP framework Laravel. When we call the deleteRecord method, this method takes an id parameter which identifies the record to be deleted. We find the specific record from the database using CrudModel::find($id) then delete the record using the delete() function.
If the record is successfully deleted, the user is given a success message that says "The record was successfully deleted." If the record is not deleted for some reason, an error message is given that says "The record was not successfully deleted." These messages tell the user whether the operation is successful or not.