Dkna
Dkna

Reputation: 437

CSRF token not found in laravel

I am getting this CSRF token error and when I look around everyone just said that all I need is to use {{ csrf_field() }} when I submit my data. In the past, it was working fine but after some time, I got this error and I can't submit my data or even edit my data at all. Can someone help me here thanks.

enter image description here

Please tell me where did I go wrong? What am I suppose to do here

edit.blade.php (this is the csrf part)

        <form class="form-horizontal" method="post" action="{{ url('/user/show/'.$object->id) }}">

          {{ method_field('PUT')  }}
          {{ csrf_field() }}

         <div class="input-group">
            <label><b>Name/NRIC:</b></label>
              <input type="text" name="Name" value="{{ $object->Name }}" class="form-control">
        </div>
</form>

app.js (laravel default not mine, this is what I saw)

/**
 * We'll load the axios HTTP library which allows us to easily issue requests
 * to our Laravel back-end. This library automatically handles sending the
 * CSRF token as a header based on the value of the "XSRF" token cookie.
 */

window.axios = __webpack_require__(16);

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

/**
 * Next we will register the CSRF Token as a common header with Axios so that
 * all outgoing HTTP requests automatically have it attached. This is just
 * a simple convenience so we don't have to attach every token manually.
 */

var token = document.head.querySelector('meta[name="csrf-token"]');

if (token) {
  window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
  console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}

EDIT: Not sure if this is related but since it is the login part just want to know if it affects this.

I have actually 2 app.blade.php for the layout so was wondering is that the reason which is causing this whole problems?

Upvotes: 1

Views: 10475

Answers (5)

Abdelsalam Shahlol
Abdelsalam Shahlol

Reputation: 1769

All you have to do is to check your generated HTML file ctrl+U. You will notice that your HTML <head> tag is probably placed inside of the body tag. This happens if you don't get your layout and blade sections right.

This happened to me before

@extends('layouts.main-layout')
  <h1>Laravel Project</h1> <--This will break the **generated template**
@section('content')
    <div id="app"></div>
@endsection

The <h1> will break template you will see the CSRF-token error in the console.

Upvotes: 0

Mostafa Aabed
Mostafa Aabed

Reputation: 11

open your inspector and make sure the meta tag is placed in head . if you find meta placed in body , so you may include script out of section field in your section script, you must include into section field.

@extends('app')
@section('content')
@include('sidebar')

Upvotes: 0

Bugfixer
Bugfixer

Reputation: 2617

Start and end of your form

{!!
    Form::
    open(
    array(
    'id'=>'FormIF',
    'name'=>'FormName',
    'autocomplete'=>'off' ,
    'url' =>route('route_to_post_method')
    )
    )
    !!}

//Form Body

{!! Form::close() !!}

Upvotes: 0

FULL STACK DEV
FULL STACK DEV

Reputation: 15971

you need to add this into your head tag

 <meta name="csrf-token" content="{{csrf_token}}">

this error will not be shown anymore.

for more details go here for more details

Upvotes: 3

Leo
Leo

Reputation: 7420

Go to your master blade or app.blade.php and make sure you add this :

<meta name="csrf-token" content="{{ csrf_token() }}">

Upvotes: 5

Related Questions