MassAssignmentException in Laravel

Here is an example of how a MassAssignmentException might be triggered in Laravel:

class User extends Model {
    protected $fillable = ['name', 'email', 'password'];

$user = new User();
$user->fill(['name' => 'John Doe', 'email' => '[email protected]', 'password' => 'secret', 'admin' => true]);

Watch a course Learn object oriented PHP

In this example, the User model has been configured to only allow mass-assignment for the 'name', 'email', and 'password' attributes. However, when the fill() method is called and passed an array that includes the 'admin' attribute, it will trigger a MassAssignmentException because 'admin' is not included in the $fillable array. To fix this, you would need to add 'admin' to the $fillable array like this

class User extends Model {
    protected $fillable = ['name', 'email', 'password','admin'];

This way, the code will not throw any exception.