How to call a Controller Function on Button Click in Laravel

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.

php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Random tutorial links