Fixed – MassAssignmentException in model.php laravel?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this post i will show you how to Fixed – MassAssignmentException in model.php laravel?massassignmentexception is for the security purpose if we use laravel function. When i was start Laravel new project and first i was try to add a user by using “User::create([‘name’=>’hd’,’password’=>’1w’])” then i was found the bellow error. On that time I was thinking about this what was wrong, but after the investigation i was found it for our security purpose, because we have to add field name that we want to insert that way other Http script or hacker etc scan’t insert something else we need to add the all item as $fillable variable in our Model.

So, let’s see our user model example.

User.php

namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','facebook_id'
    ];


    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];


    public function dataCreate($input)
    {
        return static::create($input);
    }
}

Read Also : Laravel Stripe example by using Laravel Cashier from Scratch

Thanks for read. I hope it help you. For more you can follow us on facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →