Search code examples
phplaraveldynamicmodelsave

Laravel: How do i fix this trait (with attributes) to also work when creating a model?


I'm using a trait to dynamically add e-mail attribute(s) to a model. It gives me the possibility to reuse code amongst many models. However, this code fails when i try to create a new model (but succeeds when i update an existing model).

The issue is the assumption that $this->id is available in Traits/Contact/HasEmails > setEmailTypeAttribute. Id is not yet available, because saving is not finished.

My question: How can i fix this trait to also work when creating a model?

Google, no results Thinking about something of model events (static::creating($model))

\app\Traits\Contact\HasEmails.php


 /*
     * EmailGeneric getter. Called when $model->EmailGeneric is requested.
     */
    public function getEmailGenericAttribute() :?string
    {
        return $this->getEmailTypeAttribute(EmailType::GENERIC);
    }

    /*
     * EmailGeneric setter. Called when $model->EmailGeneric is set.
     */
    public function setEmailGenericAttribute($email)
    {
        return $this->setEmailTypeAttribute(EmailType::GENERIC, $email);
    }

     /*
     * Get single e-mail model for model owner
     */
    private function getEmailTypeAttribute($emailType) :?string
    {
        $emailModel = $this->emailModelForType($emailType);
        return $emailModel ? $emailModel->email : null;
    }

    /*
    * Update or create single e-mail model for model owner
    *
    * @return void
    */
    private function setEmailTypeAttribute($emailType, $email) :void
    {
        $this->emails()->updateOrCreate([
            'owned_by_type' => static::class,
            'owned_by_id' => $this->id,
            'type' => $emailType
        ],['email' => $email]);
    }

\app\Models\Email.php


namespace App\Models;

class Email extends Model
{

    public $timestamps = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'email'
    ];

    /*
     * Get polymorphic owner
     */
    public function ownedBy(): \Illuminate\Database\Eloquent\Relations\MorphTo
    {
        return $this->morphTo();
    }

    /*
     * Default attributes are prefilled
     */
    protected function addDefaultAttributes(): void
    {
        $attributes = [];
        $attributes['type'] = \App\Enums\EmailType::GENERIC;

        $this->attributes = array_merge($this->attributes, $attributes);
    }
}

\migrations\2019_10_16_101845_create_emails_table.php

 Schema::create('emails', function (Blueprint $table) {
            $table->bigIncrements('id');

            $table->unsignedBigInteger('owned_by_id');
            $table->string('owned_by_type');

            $table->string('type');              //f.e. assumes EmailType
            $table->string('email');

            $table->unique(['owned_by_id', 'owned_by_type', 'type'], 'owner_type_unique');
        });

I expect a related model to be created/updated, but it fails on creating.


Solution

  • Trick was using a saved model event and also not forgetting to set the fillable attribute on the email model:

    /*
        * Update or create single e-mail model for model owner
        *
        * @return void
        */
        private function setEmailTypeAttribute($emailType, $email) :void
        {
            static::saved(static function($model) use($emailType, $email) {
                $model->emails()
                    ->updateOrCreate(
                        [
                            'owned_by_type' => get_class($model),
                            'owned_by_id' => $model->id,
                            'type' => $emailType
                        ],
                        [
                            'email' => $email
                        ]);
                });
        }