Problem
I’m fresh to Laravel. I’d like to start seeding my database. I get an exception when I perform the seed command.
[Illuminate\Database\Eloquent\MassAssignmentException]
username
db:seed [--class[="..."]] [--database[="..."]]
What am I doing incorrectly? I use the following command:
php artisan db:seed --class="UsersTableSeeder"
The following is my seed class:
class UsersTableSeeder extends Seeder {
public function run()
{
User::truncate();
User::create([
'username' => 'PaulSheer',
'email' => 'psheer@rute.co.za',
'password' => '45678'
]);
User::create([
'username' => 'Stevo',
'email' => 'steve@rute.co.za',
'password' => '45678'
]);
}
}
Asked by user1801060
Solution #1
http://laravel.com/docs/eloquent#mass-assignment Read this section of the Laravel documentation: http://laravel.com/docs/eloquent#mass-assignment
By default, Laravel protects against mass assignment security concerns. That’s why you have to specify which fields can be “bulk allocated” manually:
class User extends Model
{
protected $fillable = ['username', 'email', 'password'];
}
Be cautious when allowing the mass assignment of essential fields such as passwords or roles. It may constitute a security risk since users may be able to change the values of this field when you don’t want them to.
Answered by Alexandre Butynski
Solution #2
I’m working with Laravel 4.2.
the problem you’re having
[Illuminate\Database\Eloquent\MassAssignmentException]
username
This is because the database is secured against mass filling, which is exactly what you’re doing when you run a seeder. However, in my opinion, declaring which fields in your model should be fillable is not necessary (and may be insecure) if you only need to run a seeder.
The DatabaseSeeder class can be found in your seeding folder:
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Eloquent::unguard();
//$this->call('UserTableSeeder');
}
}
This class serves as a front, listing all of the seeders that must be run. This DatabaseSeeder class is bypassed if you manually call the UsersTableSeeder seeder through artisan, as you did with the php artisan db:seed —class=”UsersTableSeeder” command.
The method Eloquent::unguard(); in this DatabaseSeeder class permits temporary mass assignment on all tables, which is exactly what you need when seeding a database. This unguard method is only used when you run the php aristan db:seed command, so it’s only used once rather than making the fields in your model fillable (as stated in the accepted and other answers).
Simply add $this->call(‘UsersTableSeeder’); to the run method in the DatabaseSeeder class, and run php aristan db:seed in your CLI, which will execute DatabaseSeeder by default.
Also, you’re using the plural word Users, when Laraval uses the singular form User. If you decide to change your class to the conventional singular form, you can simply uncomment the //$this->call(‘UserTableSeeder’); which has already been assigned but commented out by default in the DatabaseSeeder class.
Answered by Pascalculator
Solution #3
Declare the following on your class to make all fields fillable:
protected $guarded = array();
This allows you to use the fill method without having to declare each field.
Answered by Tiago Gouvêa
Solution #4
When doing a seed, simply add Eloquent::unguard(); to the top of the run function; no need to build a $fillable array in all the models you need to seed.
Normally, the DatabaseSeeder class includes this information. However, because you’re directly invoking the UsersTableSeeder:
—class=”UsersTableSeeder” php artisan db:seed
The issue occurs because Eloquent::unguard(); is not called.
Answered by bicycle
Solution #5
I used it and had no issues:
protected $guarded=[];
Answered by parastoo
Post is based on https://stackoverflow.com/questions/22280136/massassignmentexception-in-laravel