In this tutorial we will discuss, how to call a controller function when click on button in laravel.
Controller function call on button click
For calling a controller function we make a route in web.php and give its url on button click see in example.
web.php
<?php Route::get('user-delete/{id}',[UserController::class,'delete'])->name('user.delete');
UserController
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class UserController extends Controller{ public function delete(Request $request){ $id = $request->id; User::where('id', $id)->delete(); } }
Blade file
<button class="btn"><a href="{{ route('user.delete', $id) }}">Delete User</a></button>
You read this tutorial on advanced web tuts provided by anmol sharma.