How to check if form submitted in laravel?

Member

by lottie , in category: PHP Frameworks , 7 months ago

How to check if form submitted in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by ryleigh , 7 months ago

@lottie 

In Laravel, you can check if a form has been submitted by checking if the request method is POST and if the form token matches the CSRF token.


You can do this by using the following code in your controller or route handler:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
use IlluminateHttpRequest;

public function submitForm(Request $request)
{
    if ($request->isMethod('post') && $request->has('_token')) {
        // Form has been submitted
        // Your code here
    } else {
        // Form has not been submitted
    }
}


This code checks if the request method is POST and if the CSRF token is present in the form data. If both conditions are met, it means that the form has been submitted. You can then perform any necessary actions with the form data inside the if statement block.