'date', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; protected $attributes = [ 'blood_type' => 'unknown', ]; protected static function boot() { parent::boot(); static::creating(function ($model) { if (empty($model->medical_record_number)) { $model->medical_record_number = self::generateMedicalRecordNumber(); } }); } public static function generateMedicalRecordNumber() { $prefix = 'MRN'; $year = Carbon::now()->format('Y'); $month = Carbon::now()->format('m'); $sequence = self::whereYear('created_at', $year) ->whereMonth('created_at', $month) ->count() + 1; return sprintf('%s-%s%s-%04d', $prefix, $year, $month, $sequence); } }