#2114·larastan

在解析迁移时添加对读取公共常量的支持

作者: patrick-leb创建于 2024年11月17日更新于 2026年9月1日
标签enhancement

class Compensation extends Model { use HasFactory; use SoftDeletes; public const ATTR_AMOUNT = 'amount'; protected $casts = [ 'id' => 'integer', 'amount' => 'float', ]; protected $fillable = [ 'amount', ]; public function toHourlyRate(float $weeklyHours): float { return match ($this->{self::ATTR_PERIOD}) { CompensationPeriod::HOURLY => $this->{self::ATTR_AMOUNT}, CompensationPeriod::DAILY => $this->{self::ATTR_AMOUNT} * 5 / $weeklyHours, CompensationPeriod::WEEKLY => $this->{self::ATTR_AMOUNT} / $weeklyHours, CompensationPeriod::BIWEEKLY => $this->{self::ATTR_AMOUNT} / ($weeklyHours * 2), CompensationPeriod::BIMONTHLY => $this->{self::ATTR_AMOUNT} * 24 / ($weeklyHours * 52), CompensationPeriod::MONTHLY => $this->{self::ATTR_AMOUNT} * 12 / ($weeklyHours * 52), CompensationPeriod::QUARTERLY => $this->{self::ATTR_AMOUNT} * 4 / ($weeklyHours * 52), CompensationPeriod::SEMIANNUALLY => $this->{self::ATTR_AMOUNT} * 2 / ($weeklyHours * 52), CompensationPeriod::ANNUALLY => $this->{self::ATTR_AMOUNT} / ($weeklyHours * 52), default => throw new InvalidArgumentException(__('Invalid compensation period')), ]; } } Migration file, located in the default path database/migrations

php
public function up(): void
{
    Schema::create('compensation', function (Blueprint $table) {
        $table->id();
        [...]
        $table->decimal(Compensation::ATTR_AMOUNT, 10, 2, 2);
        $table->timestamps();
    });
}

内容来源: larastan/larastan