Update non fillable field in Laravel?

3.3k views Asked by At

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.

2

There are 2 answers

2
Alexandre Elshobokshy On BEST ANSWER

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.

0
Clément Baconnier On

As I suggested in the comment; I feel more like you're doing something equivalent to

foreach(User::all() as $user) {
    $user->password = User::find($user->id)->password; 
    $user->save();
}

which won't change anything.