Laravel 5: when saving form data, _token throws a mass assignment exception

When I try to save form data, Laravel throws a mass assignment exception.

In the view, I use {!! form::open(...) !!} {!! form::open(...) !!} , which, as I know, creates _token as a hidden field.

When form data is submitted to the controller, I use

 $data = Input::all(); $order = Order::create($data); $order->save(); 

Should I add a field for _token to my database? Or am I causing an error by doing something else wrong?

+11
source share
4 answers

The mass assignment exception is usually caused by the fact that you did not specify fillable attributes in your model (or guarded opposite). Do it:

 class Order extends Eloquent { protected $fillable = ['field1', 'foo', 'bar']; } 

Thus, you also do not need to worry about _token , because only the specified fields will be filled and stored in db no matter what other material you pass to the model.

+25
source

or

 protected $guarded = array(); 
+5
source

Make sure that you put $ fillable or $ guard in the app \ Order.php file, and not in the app \ Http \ Controllers \ OrderController.php file.

0
source

To answer your initial question. Would you like to do unset ($ request ['_ token']); before your creation.

0
source

Source: https://habr.com/ru/post/984846/


All Articles