7 Replies - 447 Views - Last Post: 09 May 2019 - 10:11 AM Rate Topic: -----

#1 lfrost2015   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 08-March 15

Laravel Token Mismatch CSRF error

Posted 02 May 2019 - 01:42 PM

In one of my projects in Laravel I have implemented CRUD functionality and in the views for each function I have put a csrf token and a request in depending on the function i.e. PUT for updating something. I have two problems unfortunately from this. It seems to put @csrf and @method(PUT) in plain text on the html page rather than actually adding the respective functionality to the page. Also for example on the edit page when I try to edit an existing entry in the database, a token mismatch exception error appears and I'm not sure why.

Edit action in the controller:
    public function update(Request $request, $id)
    {
        $blog = Blog::find($id);
        $blog->title = $request->title;
        $blog->content = $request->content;
        $blog->update();

        return redirect()->route('blog_path', ['blog' => $blog]);
    }



Form on edit.blade.php:
<form action="{{ route('update_blog_path', ['blog' => $blog->id]) }}" method="POST">
  <input type="hidden" name="_method" value="PUT">
  @csrf

  @method('PUT')

  <div class="form-group">
    <label for="title">Title</label>
    <input type="text" name="title" class="form-control" value="{{ $blog->title}}">
  </div>

  <div class="form-group">
    <label for="content">Content</label>
    <textarea name="content" rows="10" class="form-control">{{$blog->content}}</textarea>
  </div>

  <div class="form-group">
    <button type="submit" class="btn btn-outline-primary">Edit Blog Post</button>
  </div>


</form>



The token mismatch error also doesn't allow me to login and register with standard Laravel authentication so I was wondering if it is a simple fix to solve this error or whether there is something more to it than that?

Is This A Good Question/Topic? 0
  • +

Replies To: Laravel Token Mismatch CSRF error

#2 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2282
  • View blog
  • Posts: 6,971
  • Joined: 15-January 14

Re: Laravel Token Mismatch CSRF error

Posted 02 May 2019 - 03:20 PM

You're probably supposed to put the token in a hidden input in the form.
Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,870
  • Joined: 08-August 08

Re: Laravel Token Mismatch CSRF error

Posted 03 May 2019 - 09:15 PM

Try something like this.
<input type="hidden" name="_token" value="{{ csrf_token() }}">

Was This Post Helpful? 0
  • +
  • -

#4 lfrost2015   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 08-March 15

Re: Laravel Token Mismatch CSRF error

Posted 05 May 2019 - 10:38 AM

View PostCTphpnwb, on 03 May 2019 - 09:15 PM, said:

Try something like this.
<input type="hidden" name="_token" value="{{ csrf_token() }}">


Put this in and it comes up with a new error which is a MethodNotAllowedHttpException error.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3814
  • View blog
  • Posts: 13,870
  • Joined: 08-August 08

Re: Laravel Token Mismatch CSRF error

Posted 05 May 2019 - 11:48 AM

That sounds like your route isn't set up to allow POST.
Was This Post Helpful? 0
  • +
  • -

#6 lfrost2015   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 08-March 15

Re: Laravel Token Mismatch CSRF error

Posted 07 May 2019 - 08:41 AM

View PostCTphpnwb, on 05 May 2019 - 11:48 AM, said:

That sounds like your route isn't set up to allow POST.


Here are the defined routes in web.php:

Route::name('blogs_path')->get('/blogs', '[email protected]');
Route::name('create_blog_path')->get('/blogs/create', '[email protected]');
Route::name('store_blog_path')->post('/blogs', '[email protected]');
Route::name('blog_path')->get('/blogs/{id}', '[email protected]');
Route::name('edit_blog_path')->get('/blogs/{id}/edit', '[email protected]');
Route::name('update_blog_path')->put('/blogs/{id}', '[email protected]');
Route::name('delete_blog_path')->delete('/blogs/{id}', '[email protected]');




As you can see I have put for the update action. I'm assuming this should be changed to Post?
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier   User is online

  • D.I.C Lover
  • member icon

Reputation: 2282
  • View blog
  • Posts: 6,971
  • Joined: 15-January 14

Re: Laravel Token Mismatch CSRF error

Posted 07 May 2019 - 10:49 AM

Change it to post and see what happens.
Was This Post Helpful? 1
  • +
  • -

#8 lfrost2015   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 08-March 15

Re: Laravel Token Mismatch CSRF error

Posted 09 May 2019 - 10:11 AM

View PostArtificialSoldier, on 07 May 2019 - 10:49 AM, said:

Change it to post and see what happens.


Sorted! Thanks for that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1