From 4fd2d11137eb276a32bb7c14ef9e76823fc03896 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Wed, 28 Jan 2026 01:19:47 -0600 Subject: [PATCH] Phone number cast --- app/Casts/PhoneNumberCast.php | 33 +++++++++++++++++++ app/Models/Contact.php | 5 +++ database/factories/ClientFactory.php | 5 ++- database/factories/ContactFactory.php | 5 ++- ..._28_023011_create_client_contact_table.php | 4 +-- 5 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 app/Casts/PhoneNumberCast.php diff --git a/app/Casts/PhoneNumberCast.php b/app/Casts/PhoneNumberCast.php new file mode 100644 index 0000000..9e3214a --- /dev/null +++ b/app/Casts/PhoneNumberCast.php @@ -0,0 +1,33 @@ + substr($value, 0, 3).'-'.substr($value, 3), + 10 => '('.substr($value, 0, 3).') '.substr($value, 3, 3).'-'.substr($value, 6), + default => strlen($value) > 10 + ? '+'.substr($value, 0, -10).' ('.substr($value, -10, 3).') '.substr($value, -7, 3).'-'.substr($value, + -4) + : $value, + }; + } + + public function set(Model $model, string $key, $value, array $attributes) + { + if ($value === null) { + return null; + } + + return preg_replace('/\D/', '', $value); + } +} diff --git a/app/Models/Contact.php b/app/Models/Contact.php index f463287..24099c6 100644 --- a/app/Models/Contact.php +++ b/app/Models/Contact.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Casts\PhoneNumberCast; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -13,6 +14,10 @@ class Contact extends Model public $fillable = ['first_name', 'last_name', 'email', 'phone']; + public $casts = [ + 'phone' => PhoneNumberCast::class, + ]; + public function clients(): BelongsToMany { return $this->belongsToMany(Client::class); diff --git a/database/factories/ClientFactory.php b/database/factories/ClientFactory.php index 1aca132..bbc7a0a 100644 --- a/database/factories/ClientFactory.php +++ b/database/factories/ClientFactory.php @@ -27,7 +27,10 @@ class ClientFactory extends Factory public function withContact(?Contact $contact = null): static { return $this->afterCreating(function (Client $client) use ($contact) { - $client->contacts()->attach($contact ?? Contact::factory()->create()); + $client->contacts()->attach( + $contact ?? Contact::factory()->create(), + ['is_primary' => true] + ); }); } } diff --git a/database/factories/ContactFactory.php b/database/factories/ContactFactory.php index 44a4b31..a4f65af 100644 --- a/database/factories/ContactFactory.php +++ b/database/factories/ContactFactory.php @@ -23,7 +23,10 @@ class ContactFactory extends Factory public function withClient(?Client $client = null): static { return $this->afterCreating(function (Contact $contact) use ($client) { - $contact->clients()->attach($client ?? Client::factory()->create()); + $contact->clients()->attach( + $client ?? Client::factory()->create(), + ['is_primary' => true] + ); }); } } diff --git a/database/migrations/2026_01_28_023011_create_client_contact_table.php b/database/migrations/2026_01_28_023011_create_client_contact_table.php index b5c8932..46f97b0 100644 --- a/database/migrations/2026_01_28_023011_create_client_contact_table.php +++ b/database/migrations/2026_01_28_023011_create_client_contact_table.php @@ -15,8 +15,8 @@ return new class extends Migration { Schema::create('client_contact', function (Blueprint $table) { $table->id(); - $table->foreignIdFor(Client::class)->constrained(); - $table->foreignIdFor(Contact::class)->constrained(); + $table->foreignIdFor(Client::class)->constrained()->cascadeOnDelete(); + $table->foreignIdFor(Contact::class)->constrained()->cascadeOnDelete(); $table->boolean('is_primary')->default(false); $table->timestamps();