'integer', 'versione_nuova' => 'integer', 'dati_precedenti' => 'array', 'dati_nuovi' => 'array', 'diff' => 'array', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; /** * Relazione con User */ public function utente() { return $this->belongsTo(User::class, 'utente_id'); } /** * Relazione polimorfica con entità */ public function entita() { return $this->morphTo(); } /** * Scope per entità */ public function scopePerEntita($query, $entita, $entitaId) { return $query->where('entita', $entita)->where('entita_id', $entitaId); } /** * Genera diff stile GIT */ public static function generaDiff($datiPrecedenti, $datiNuovi) { $diff = []; foreach ($datiNuovi as $campo => $valoreNuovo) { $valorePrecedente = $datiPrecedenti[$campo] ?? null; if ($valorePrecedente != $valoreNuovo) { $diff[] = [ 'campo' => $campo, 'da' => $valorePrecedente, 'a' => $valoreNuovo, 'tipo' => $valorePrecedente === null ? 'aggiunto' : 'modificato', ]; } } return $diff; } }