How to solve the page expired (419) error in Laravel?

How to solve the page expired (419) error in Laravel?

Hey Guys,

Many times we got the “Page Expired” error in Laravel.

Condition 1:

If you are getting an error after submitting the form then you need to add the CSRF field in your form.

<form method="POST" action="/profile">
    @csrf
    ...
</form>

Condition 2:

If you are getting an error after calling the AJAX then you need to add a header like below.

In your head tag

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

In Your Script tag

    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});