vendredi 15 février 2019

Laravel 5.7 Attach an Email to contact_info on a Person where through eloquent relationships

I have a Person model with a one-to-many relationship to a ContactInfo model that has a polymorphic one-to-many relationship to a few contact type models (Email, Phone, Address) and I'm attempting to create a person with an email in a way that assigns all of the relationships appropriately. The line My current attempt:

$person = \App\Models\Person::create($request->person);       
$person->contactInfo->contactable()->attach(Email::create(['email' => $request->contactInfo['email']]));

The person to contact relationship is a really straight forward Person/hasMany(contact_info) and ContactInfo/belongsTo(Person) and I've created a contactable morphTo on the ContactInfo model:

public function contactable()
{
    return $this->morphTo();
}

and then each of the contact type models (Email, Phone, address) have a morphMany to contactInfo:

public function contactInfo()
{
    return $this->morphMany('App\Models\ContactInfo', 'contactable');
}

Anyone see what's missing here? My schemas are all pretty straightforward, aside from maybe the contact_info table:

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



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

Aucun commentaire:

Enregistrer un commentaire