vendredi 15 février 2019

Laravel 5.7/Eloquent Person relationship to many Contact types

I'm attempting to design a schema with the following models: Person, Email, Phone, Address and ContactInfo. A Person can have many of each of Email, Phone, Address (and theoretically any other contact type), but I would like to simplify it so you could just grab all of a person's ContactInfo. At first glance I thought it would be a One-to-Many Person->ContactInfo and then a One-to-Many Polymorphic ContactInfo->Contactable. Contactable being a morphTo() which would create a contactable_id and contactable_type in the ContactInfo table. I'm not really sure how to implement this or if it's even the best way. Based on the above concept and laravel's docs, I would do the following.

Person:
**
 * One-to-many relationship to ContactInfo
 *
 * @return \Illuminate\Database\Eloquent\Relations\hasMany
 */
public function contactInfo()
{
    return $this->hasMany(ContactInfo::class);
}

ContactInfo:
/**
 * One-to-many polymorphic relationship to Contact types (Email, Phone, Address, etc.)
 *
 * @return \Illuminate\Database\Eloquent\Relations\morphTo
 */
public function contactable()
{
    return $this->morphTo();
}
Example Email:
/**
 * Many-to-one polymorphic relationship to Person
 *
 * @return \Illuminate\Database\Eloquent\Relations\morphMany
 */
public function contactInfo()
{
    return $this->morphMany('App\Models\ContactInfo', 'contactable');
}

And then my contact_info migration:

Schema::create(
    'contact_info', function (Blueprint $table) {
        $table->increments('id');
        $table->uuid('person_id');
        $table->index('person_id');
        $table->foreign('person_id')->references('id')->on('people');
        $table->morphs('contactable');
        $table->boolean('is_primary')->default(false);
        $table->boolean('is_active')->default(true);
        $table->timestamps();
        $table->softDeletes();
    }
);

Then if I were to create a person and their contact_info through relationships, it might look something like this:

$person = \App\Models\Person::create($request->person);
$contactInfo = new \App\Models\ContactInfo();
$contactInfo->person_id = $person->id;
$contactInfo->contactable()->attach(Email::create(['email' => $request->contactInfo['email']]));
$contactInfo->contactable()->attach(Phone::create(['number' => $request->contactInfo['homePhone'], 'type' => 'Home']));
$contactInfo->contactable()->attach(Phone::create(['number' => $request->contactInfo['workPhone'], 'type' => 'Work']));
$contactInfo->contactable()->attach(Phone::create(['number' => $request->contactInfo['fax'], 'type' => 'Fax']));
$contactInfo->save();

But this doesn't look quite right and when I attempt this with correct data, I get a person and the individual contact type models to store, but the contact_info table does not store any records and thusly the relationships are lost. I'm 100% not tied to this set-up. At the end of the day, I just want to find the best solution for storing a Person and any/all contact_types they may have as relationships, hopefully leveraging polymorphism to allow for extensible types. Any advice would be awesome!



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2GK6eBg
via IFTTT

Aucun commentaire:

Enregistrer un commentaire