From 0780d7589b32d701f1075bcbb7e6c89c5fb5fb2c Mon Sep 17 00:00:00 2001 From: Matt Young Date: Tue, 27 Jan 2026 20:44:16 -0600 Subject: [PATCH] Client Contact pivot table and relatioships --- app/Models/Client.php | 21 +++++++++++ app/Models/Contact.php | 7 ++++ ..._28_023011_create_client_contact_table.php | 35 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 database/migrations/2026_01_28_023011_create_client_contact_table.php diff --git a/app/Models/Client.php b/app/Models/Client.php index 8a80e1b..82a818a 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -4,6 +4,7 @@ namespace App\Models; use App\Enums\ClientStatus; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; class Client extends Model { @@ -18,4 +19,24 @@ class Client extends Model 'audition_date' => 'date', 'status' => ClientStatus::class, ]; + + public function contacts(): BelongsToMany + { + return $this->belongsToMany(Contact::class) + ->withPivot('is_primary'); + } + + public function primaryContact(): BelongsToMany + { + return $this->belongsToMany(Contact::class) + ->wherePivot('is_primary', true) + ->withPivot('is_primary'); + } + + public function secondaryContacts(): BelongsToMany + { + return $this->belongsToMany(Contact::class) + ->wherePivot('is_primary', false) + ->withPivot('is_primary'); + } } diff --git a/app/Models/Contact.php b/app/Models/Contact.php index 98d1b44..7aede8e 100644 --- a/app/Models/Contact.php +++ b/app/Models/Contact.php @@ -3,8 +3,15 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasMany; class Contact extends Model { public $fillable = ['first_name', 'last_name', 'email', 'phone']; + + public function clients(): BelongsToMany + { + return $this->belongsToMany(Client::class); + } } 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 new file mode 100644 index 0000000..af74b7f --- /dev/null +++ b/database/migrations/2026_01_28_023011_create_client_contact_table.php @@ -0,0 +1,35 @@ +id(); + $table->foreignIdFor(Client::class); + $table->foreignIdFor(Contact::class); + $table->boolean('is_primary')->default(false); + $table->timestamps(); + + // Ensure a relationship is unique + $table->unique(['client_id', 'contact_id']); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('client_contact'); + } +};