phplaraveleloquentfillable

Update non fillable field in Laravel?


I would like to import data from an old database, so I would like to fill some fields such as password. The User model looks like this:

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

In my migration I have:

class ThumbnailSeeder extends Seeder
{
    public function run()
    {
        foreach(User::all() as $user) {
            $user->password = get_old_password($user->id);
            $user->save();
        }
    }
}

Obviously this doesn't work because Laravel think I am doing Mass assignment...

What should I change to make this work?

I've looked at other similar question such as this one, but I still not figured out how to bypass Laravel protection.


Solution

  • You can try using forceFill.

    foreach(User::all() as $user) {
        $user->forceFill(array('password ' => get_old_password($user->id));
        $user->save(); //Not sure if this is necessary
    }
    

    This is untested code.