Hello Devs, 

In this tutorial, we will learn How To Generate Csrf Token In Laravel

Follow this step-by-step guide below. 


Example 1:@csrf

Syntax

<form method="POST">
  @csrf  // Generate hidden input field
  .....
  .....
</form>


Example

<!DOCTYPE html> 
<html> 
  <head> 
    <title>Laravel | CSRF Protection</title> 
  </head> 
  <body> 
    <section> 
      <h1>CSRF Protected HTML Form</h1> 
      <form method="POST"> 
        @csrf 
        <input type="text" name="username" placeholder="Username"> 
        <input type="password" name="password" placeholder="Password"> 
        <input type="submit" name="submit" value="Submit"> 
      </form> 
    </section> 
  </body> 
</html> 


Example 2:csrf_field()

Syntax:

<form method="POST">
  {{ csrf_field() }}
  .....
  .....
</form>


Example

<!DOCTYPE html> 
<html> 
  <head> 
    <title>Laravel | CSRF Protection</title> 
  </head> 
  <body> 
    <section> 
      <h1>CSRF Protected HTML Form</h1> 
      <form method="POST"> 
        {{ csrf_field() }}
        <input type="text" name="username" placeholder="Username"> 
        <input type="password" name="password" placeholder="Password"> 
        <input type="submit" name="submit" value="Submit"> 
      </form> 
    </section> 
  </body> 
</html>


Example 3:csrf_token()

Syntax:

<form method="POST">
  <input type="hidden" name="_token" value="{{ csrf_token() }}">
  .....
  .....
</form>


Example

<!DOCTYPE html> 
<html> 
  <head> 
    <title>Laravel | CSRF Protection</title> 
  </head> 
  <body> 
    <section> 
      <h1>CSRF Protected HTML Form</h1> 
      <form method="POST"> 
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
        <input type="text" name="username" placeholder="Username"> 
        <input type="password" name="password" placeholder="Password"> 
        <input type="submit" name="submit" value="Submit"> 
      </form> 
    </section> 
  </body> 
</html>


May this example help you.