Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel TokenMismatchException

Tags:

php

laravel

Right now I'm learing laravel but I keep getting the exeption:

TokenMismatchException in VerifyCsrfToken.php line 53:

I'm trying to make an object of a migration and then write it to the database but for some reason it's not working. This is my route.php:

Route::get('/post/new',array(
'uses'=> 'blog@newPost',
'as' => 'newPost'
    ));
Route::post('/post/new', array (
'uses' => 'blog@createPost',
'as' => 'createPost'
    ));

This is my controller called blog.php:

use Illuminate\Http\Request;

use App\Http\Requests;
use View;
use App\Http\Controllers\Controller;
use App\posts;

    class blog extends Controller
    {
       public function newPost() 
       {
          return View::make('new');
       }

       public function createPost() 
       {
            $posts = new posts();
            $posts->title = Input::get('title');
            $posts->content = nl2br(Input::get('content'));
            $posts->save();
       }
    }

This is the migration:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts',function($table) {
            $table->increments('id');
            $table->string('title');
            $table->text('content');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        schema::drop('posts');
    }
}

And this is my main view:

@extends('master')
@section('content')
<h3>Add a blog post</h2>
 <form action="{{ URL::route('createPost') }}" method="post">
 <div class="form-group">
 <input name="title" class="form-control" type="text" placeholder="title"/>
 </div>
 <div class="form-group">
 <textarea name="content" class="form-control" placeholder="write here"> </textarea>
 </div>
 <input type="submit" class="btn btn-primary" />
 </form>
@stop

What could be wrong?

like image 386
Jamie Avatar asked Jul 15 '15 10:07

Jamie


2 Answers

Add this right before </form>

{!! csrf_field() !!}

Take a look at Laravel docs for more info

like image 165
Javi Stolz Avatar answered Sep 28 '22 08:09

Javi Stolz


Add this line before the closing tag of your form:

{{ Form::token() }}
like image 21
Michel Avatar answered Sep 28 '22 06:09

Michel