I have a Person model with a "contactable" morphTo relationship:
/**
* One-to-many polymorphic relationship to Contact types (Email, Phone, Address, etc.)
*
* @return \Illuminate\Database\Eloquent\Relations\morphTo
*/
public function contactable()
{
return $this->morphTo();
}
Then each model for Email, Phone, Address has a morphMany relationship to Person:
/**
* Many-to-one polymorphic relationship to Person
*
* @return \Illuminate\Database\Eloquent\Relations\morphMany
*/
public function person()
{
return $this->morphMany('App\Models\Person', 'contactable');
}
I'm curious how to set up a contact_info pivot table to store the relationship and type as Email, Phone, Address, etc. Here's where I'm at with that:
Schema::create(
'contact_info', function (Blueprint $table) {
$table->increments('id');
$table->uuid('person_id');
$table->foreign('person_id')->references('id')->on('people');
$table->string('contactable_type');
$table->boolean('is_primary')->default(false);
$table->boolean('is_active')->default(true);
$table->timestamps();
$table->softDeletes();
}
);
It is currently missing the uuid for the contact type (aka, Email, Phone, Address). I'm attempting to create these like so:
$person->contactable()->attach(Email::create(['email' => $request->contactInfo['email']]));
$person->contactable()->attach(Phone::create(['number' => $request->contactInfo['homePhone'], 'type' => 'Home']));
$person->contactable()->attach(Phone::create(['number' => $request->contactInfo['workPhone'], 'type' => 'Work']));
$person->contactable()->attach(Phone::create(['number' => $request->contactInfo['fax'], 'type' => 'Fax']));
The type for phone is not related to contact type. Anyone have any advice on this? Laravel's polymorphic Eloquent docs are clear, but the migrations aren't as such. Thanks!
from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2SEfX3c
via IFTTT
Aucun commentaire:
Enregistrer un commentaire