_token error in laravel 4 on update

2019-12-16 19:07发布

问题:

I am running a laravel 4 framework and I get a Whoops! error with just the message "_token" when I try to update an instance of an eloquent model class. What does this mean? I use Form::model to create the form and I am able to update records of other model classes.

回答1:

This is usually a result of either a missing or mismatched CSRF token. If you haven't placed a token in your form, do so by echoing the function 'Form::token()' somewhere inside your form. Otherwise I believe this is handled for you if you use the 'Form::open()' syntax to open your form. You can learn more about it here: http://laravel.com/docs/html#csrf-protection

The token is stored as part of the user's session, btw, so it's possible you can get a token mismatch error if you store your sessions in the database, then wipe the database in between getting a form and posting it.



回答2:

If I am not mistaken, token is automaticly added when you use Form::open / Form::model. In this case you are probally missing array $fillable/$guarded in your model.

You can read more about this here: http://laravel.com/docs/4.2/eloquent#mass-assignment