'decimal:2', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'deleted_at' => 'datetime', ]; // ─── Scope: filtri riutilizzabili ───────────────────────────────────── // Un "scope" è un metodo che aggiunge condizioni alla query. // Uso: Customer::active()->get() // invece di: Customer::where('status', 'attivo')->get() public function scopeActive($query) { return $query->where('status', 'attivo'); } public function scopeByType($query, string $type) { return $query->where('type', $type); } public function scopeSearch($query, string $term) { return $query->where(function ($q) use ($term) { $q->where('name', 'like', "%{$term}%") ->orWhere('email', 'like', "%{$term}%") ->orWhere('city', 'like', "%{$term}%") ->orWhere('vat_number', 'like', "%{$term}%"); }); } // ─── Accessor: trasforma il valore quando lo leggi ─────────────────── // Uso: $customer->badge_color → restituisce il colore Bootstrap public function getBadgeColorAttribute(): string { return match ($this->status) { 'attivo' => 'success', 'inattivo' => 'secondary', 'prospect' => 'warning', default => 'light', }; } public function getTypeLabelAttribute(): string { return match ($this->type) { 'privato' => 'Privato', 'azienda' => 'Azienda', default => 'N/D', }; } }