sort_order; $old = (int) ($cat->getOriginal('sort_order') ?? PHP_INT_MAX); if ($new === $old) return; // Make room: shift all records at >= new position up by 1. static::withoutGlobalScope(TenantScope::class) ->where('company_id', $cat->company_id) ->where('sort_order', '>=', $new) ->where('id', '!=', $cat->id ?? 0) ->increment('sort_order'); }); static::saved(function (self $cat) { // Renormalize to 1, 2, 3, … to close any gaps left after the move. $ids = static::withoutGlobalScope(TenantScope::class) ->where('company_id', $cat->company_id) ->orderBy('sort_order') ->orderBy('id') ->pluck('id'); foreach ($ids as $i => $id) { DB::table('labor_categories') ->where('id', $id) ->update(['sort_order' => $i + 1]); } }); } protected $casts = ['sort_order' => 'integer']; /** Localized display label — falls back through ru→en→name_ro→name. */ public function label(?string $locale = null): string { $locale = $locale ?: app()->getLocale(); return $this->{"name_{$locale}"} ?: $this->name_ro ?: $this->name ?: ''; } /** Options array keyed by canonical name (stored in labors.category). */ public static function optionsForSelect(): array { return static::orderBy('sort_order')->orderBy('name')->get() ->mapWithKeys(fn ($c) => [$c->name => $c->label()]) ->toArray(); } }