Son Tran
Son Tran

Reputation: 1536

Error when add CSRF token to Laravel form

I have a login form in Laravel:

<form class="form-signin" action="{{ URL::route('adminAuthen') }}" method="POST">
    {{ csrf_field() }}
    <h2 class="form-signin-heading">Admin Login</h2>
    <label for="inputUsername" class="sr-only">Email address</label>
    <input type="text" id="inputUsername" name="username" class="form-control" placeholder="Username" required autofocus>
    <label for="inputPassword" class="sr-only">Password</label>
    <input type="password" id="inputPassword" name="password" class="form-control" placeholder="Password" required>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>

But I always got error when visit this form:

Call to undefined function csrf_field()

How can I fix this bug?

Upvotes: 7

Views: 19448

Answers (4)

Alexander Pogor
Alexander Pogor

Reputation: 21

Update 2018-10-01

in latest versions of laravel use:

@csrf

and you'll be good to go!
P/S. i use 5.7

Upvotes: 2

Kalhan.Toress
Kalhan.Toress

Reputation: 21901

I think there is no function called csrf_field() in laravel 5, use this instead of that.

<input type="hidden" name="_token" value="{{ csrf_token() }}">

Update 2016-03-17

Laravel introduce csrf_field() in version 5.1

{{ csrf_field() }} this will generate csrf token field,

Upvotes: 19

Aditya Giri
Aditya Giri

Reputation: 1836

You can replace your {{ csrf_field() }} with this:

<input type="hidden" name="_token" value="{{ csrf_token() }}">

You might have misunderstood this because of master documentation page of laravel. I don't why they did that. But this one is what I found to be working on the laravel-5.

Upvotes: 4

Joel Hinz
Joel Hinz

Reputation: 25394

The function csrf_field() isn't in regular Laravel 5 yet, even though it's on the master documentation page. You can use the helper csrf_token() instead, see e.g. the helpers documentation, and then build the field yourself from that - or create a template for it, or similar.

Upvotes: 1

Related Questions