简体   繁体   中英

Laravel Backpack $guarded fields handling

As far as I understood (correct me if I am wrong), backpack only handles $fillable fields. Isn't the whole laravel thing is the separation between $fillable and $guarded?

MWE:

In the User.php:

class User extends Authenticatable {
    protected $guarded = ['password'];
}

In the UserCrudController.php:

public function update(UpdateRequest $request)
{
    $this->handlePasswordInput($request);
    return parent::updateCrud($request);
}

Will result in:

SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value (SQL: insert into `users` (`name`, `email`, `updated_at`, `created_at`)

While using 'strict' setting for MySQL, because 'password' field is $guarded.

Is there any proper workaround to separate $fillable and $guarded fields in Backpack?

Thank you.

You should only be using one - either $fillable or $guarded. I recommend using $fillable, it's more future-proof.

From the Laravel docs :

While $fillable serves as a "white list" of attributes that should be mass assignable, you may also choose to use $guarded. The $guarded property should contain an array of attributes that you do not want to be mass assignable. All other attributes not in the array will be mass assignable. So, $guarded functions like a "black list". Of course, you should use either $fillable or $guarded - not both.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM