62 lines
1.5 KiB
PHP
62 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
|
|
class Cliente extends Model
|
|
{
|
|
use HasApiTokens;
|
|
|
|
protected $table = 'clienti';
|
|
|
|
protected $fillable = [
|
|
'username', 'password',
|
|
'nome', 'cognome', 'ragione_sociale', 'email', 'telefono',
|
|
'indirizzo', 'cap', 'citta', 'provincia', 'note',
|
|
];
|
|
|
|
protected $hidden = [
|
|
'password',
|
|
];
|
|
|
|
protected $casts = [
|
|
'password' => 'hashed',
|
|
];
|
|
|
|
public function preventivi(): HasMany
|
|
{
|
|
return $this->hasMany(Preventivo::class);
|
|
}
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::deleting(function (Cliente $cliente): void {
|
|
// Elimina di conseguenza i preventivi del cliente (con i relativi file)
|
|
// e revoca i token API usati per l'accesso al configuratore.
|
|
$cliente->preventivi()->get()->each->delete();
|
|
$cliente->tokens()->delete();
|
|
});
|
|
}
|
|
|
|
public function getNominativoAttribute(): string
|
|
{
|
|
if ($this->ragione_sociale) {
|
|
return $this->ragione_sociale;
|
|
}
|
|
|
|
$nome = trim("{$this->nome} {$this->cognome}");
|
|
|
|
return $nome !== '' ? $nome : $this->username;
|
|
}
|
|
|
|
public function getNomeCompletoAttribute(): string
|
|
{
|
|
$nome = trim("{$this->nome} {$this->cognome}");
|
|
|
|
return $nome !== '' ? $nome : $this->username;
|
|
}
|
|
}
|