<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\UserController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [HomeController::class, 'index'])->name('home');
Route::get('users/{id}', [UserController::class, 'destroy'])->name('users.destroy');
<?php
namespace App\Http\Controllers;
use App\Models\User;
class UserController extends Controller {
public function destroy($id) {
User::find($id)->delete($id);
return response()->json([
'success' => 'Record deleted successfully!',
]);
}
}
Delete record using ajax request in Laravel Example
By Hardik Savani December 27, 2018 Category : PHP Laravel jQuery AjaxPlayUnmuteLoaded: 0.69%Fullscreen
A very few days ago, i was trying to delete record using jquery ajax request in my laravel 5.7 app. i always make delete record using jquery ajax, so i also want to delete record with ajax request in laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9.
we will create delete route with controller method(we will write delete row code using database model) and write jquery ajax code with delete post request. we also pass csrf token in jquery ajax request, otherwise it will return error like delete method not allowed.
you have to simply follow few things to make done delete record from database using ajax request. Let's follow few steps.