DrawPrizeType::None, 'weight' => 0, 'limited' => true, 'sort' => 0, ]; /** * @var array */ protected $casts = [ 'type' => DrawPrizeType::class, 'limited' => 'bool', ]; /** * @var array */ protected $fillable = [ 'draw_activity_id', 'name', 'icon', 'type', 'amount', 'weight', 'limited', 'stock', 'winnings', 'sort', ]; public static function booted() { static::updating(function ($drawActivityPrize) { if ($drawActivityPrize->weight == 0 && $drawActivityPrize->activity->isPublished()) { $weight = $drawActivityPrize->activity->prizes()->where('id', '!=', $drawActivityPrize->id)->sum('weight'); if ($weight == 0) { throw new BizException('活动奖品总权重值不能为0'); } } }); } public function activity() { return $this->belongsTo(DrawActivity::class, 'draw_activity_id'); } }