Client Contact pivot table and relatioships
This commit is contained in:
parent
b7a395dfaa
commit
0780d7589b
|
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
use App\Models\Client;
|
||||
use App\Models\Contact;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('client_contact', function (Blueprint $table) {
|
||||
$table->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');
|
||||
}
|
||||
};
|
||||
Loading…
Reference in New Issue