Anshul Mishra
Anshul Mishra

Reputation: 1716

TokenMismatchException laravel 5.1

I am trying to insert value in database after request validation, but it returns me TokenMismatchException. I am not getting why?

HTML Form

{!! Form::open(['url' => 'admin/movie', 'id' => 'tab', 'files' => true]) !!}
<div class="form-group">
    <label><strong>Movie Name</strong></label>
    <input type="text" name="name" value="{{ old('name') }}" class="form-control">
</div>

<div class="form-group">
    <label><strong>Movie Poseter</strong></label>
    <input type="file" name="poster" class="form-control">
</div>
<div class="form-group">
    <label><strong>Description</strong></label>
    <textarea name="description" rows="3" class="form-control">{{ old('description') }}</textarea>
</div>

<div class="btn-toolbar list-toolbar">
    <button class="btn btn-primary"><i class="fa fa-save"></i> Save</button>
    <button class="btn btn-danger"><i class="fa fa-ban"></i> Cancel</button>
</div>
{!! Form::close() !!}

Route

Route::resource('admin/movie', 'MovieController');

Controller

class MovieController extends Controller
{   
/**
 * Create a new movie model instance.
 *
 * @return void
 */
public function __construct(MovieModel $movie){
    $this->movie = $movie;
}
public function store(MovieRequest $request)
    {   
        $input = array_except(Input::all(), '_token');

        $destinationPath = public_path() . '/' . 'posters/';

        $poster = $request->file('poster');
        $poster_ext = $poster->guessExtension();
        $poster_name = sha1($poster->getClientOriginalName() . time()) . '.' . $poster_ext;

        if($poster->move($destinationPath, $poster_name)) {
            $input['poster'] = $poster_name;
        }      

        if($this->movie->createMovie($input)){
            return Redirect::back()->with('message', $input['name'] . ' movie has been created');
        }
    }
 }

Model

class Movie extends Model
{   
    public function createMovie($input) {
        return $this->insert($input);   
    }

}

Request

class MovieRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|unique:movies|max:50',
            'poster' => 'required',
            'description' => 'required|max:1000'
        ];
    }
}

I am new in laravel 5, I have worked with laravel4 before. I am not getting why I am getting this issue.

Upvotes: 2

Views: 76

Answers (1)

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

You didn't defined the method attribute within your form tag. You need to define the method over there is it GET, POST,PUT, or PATCH. So as you were storing value then the method will be post so kindly update your code

{!! Form::open(['url' => 'admin/movie', 'id' => 'tab', 'files' => true]) !!}

into

{!! Form::open(['url' => 'admin/movie', 'method' => 'POST' ,'id' => 'tab', 'files' => true]) !!}
                                       ^^^^^^^^^^^^^^^^^^^^

Upvotes: 1

Related Questions