0

I am trying to post data to the database via hidden field through a press of a button But the error that I am facing is 419 Page Expired

Here's My View:-

<div>
<h1> All Posts </h1>
@if(count($user) > 0)
  @foreach($user as $post)
<form action="/f" method="POST">
<input type="hidden" id="friends" name="friends" value="{{$post->id}}" />
<button type="submit" class="btn btn-primary">
                                    {{ __('chat') }}
                                </button>
</form>
<div class="well">
 <a href="/profile/1"<button class="button btn-success">chat</button>></a>
  <h3>{{$post->title}}</h3>
  <h4>{{$post->body}}</h4>

  <small>written on {{$post->created_at}}</small>
</div>

@endforeach
@else
   <p>No Forms Found</p>
@endif
</div>

Here's My Route:-

Route::post('/f', 'FriendsController@store');

Here's My Controller:-

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FriendsController extends Controller
{
    public function store(Request $request)
{   dd($request->all());
    post::create([
        'friends' => $request->friends,
        ]);
     return redirect('/profile/' . auth()->user()->id);
}
}

I might have done mistake in my store method please do check and let me know whats wrong with it I'm just a beginner -ThankYou

Anonymous Chatbox
  • 431
  • 1
  • 9
  • 18
  • Does this answer your question? [Post request in Laravel - Error - 419 Sorry, your session has expired](https://stackoverflow.com/questions/52583886/post-request-in-laravel-error-419-sorry-your-session-has-expired) – Arun A S May 16 '20 at 19:20

1 Answers1

2

Error 419 means Page expired and in laravel, it comes from not having or having invalid CSRF token when making non-GET requests. You need to add a CRSF token to your form e.g by adding @csrf just after the form declaration:

<form action="/f" method="POST">
@csrf
<input type="hidden" id="friends" name="friends" value="{{$post->id}}" />

OR this way:

<form action="/f" method="POST">
 {{csrf_field()}}
<input type="hidden" id="friends" name="friends" value="{{$post->id}}" />
djunehor
  • 941
  • 7
  • 9