Reputation: 1795
Why laravel 5 csrf_token value is empty always ?
How can i get that token value ?
I tried,
{!! csrf_token !!} , {{ csrf_token }} and
{{ Form::open() }} ....{{ Form::close() }}
MY OUTPUT
<input type="hidden" name="_token"></input>
Upvotes: 5
Views: 5611
Reputation: 2459
Try echo Form::token();
? If it doesn't work, try using php artisan generate:key
on the console.
Upvotes: 0
Reputation: 298
It's because you're not using the web group middleware. Laravel is smart enough to know that if you're not using that group a token is not necessary.
Try moving your route inside the Route::group(['middleware' => 'web']
... and tell us about it :)
Source: I made the same mistake not too long ago.
Upvotes: 13
Reputation: 1239
I stumbled across this post having spent the afternoon suddenly experiencing "The page has expired due to inactivity. " when I POSTed. When doing a "view source" all tokens were present and correct. It was only that I had included:
$("#editaddTarget input").each(function () {
$(this).val("");
});
That got fired when I launched a modal. So I learned something today and will not get back the 5 hours it took me to find this newbie clanger!
Upvotes: 1
Reputation: 1795
Thanks to all.
Finally i find solution.
On Fresh Install:
Route::get('foo', function () {
return csrf_token(); // null
});
Use this:
Route::group(['middleware' => 'web'], function () {
Route::get('bar', function () {
return csrf_token(); // works
});
});
Its Working.
Upvotes: 2