首先,根据文档:

So, to get started, you should define which model attributes you want to make mass assignable. You may do this using the $fillable property on the model.

// model里定义哪些字段可以assignable
protected $fillable = ['name'];
// 使用以下2种方法,必须在`fillable`方法里定义哪些字段可以``
$flight = App\Flight::create(['name' => 'Flight 10']);
$flight = new App\Flight(['name' => 'Flight 10']);

但是呢,如果你不用fillable 其实你也可以用非常传统的方式来对创建新的model实例:

   $user = new User();
   $user->username = $request->input('username');
   // 其他字段/属性,比如email之类的
   $user->save();

标签: fillable

添加新评论