ruvaifamasood
4 months ago

MorphOne and Model Events

Posted 4 months ago by ruvaifamasood

Hello Guys.

I'm facing a problem with Morph One relationship.

I have two models, Call (MorphOne) and Status (MorphTo).

Status Migration:

id, code, statusable_id, statusable_type

I want to create the call status when a call is created.

For that i'm using Model Events.

static::created(function ($model) {
    $model->status()->updateOrCreate([
        'code' => 1
    ]);
});             

Error:

SQLSTATE[HY000]: General error: 1364 Field 'code' doesn't have a default value (SQL: insert into `statuses` (`statusable_id`, `statusable_type`, `updated_at`, `created_at`) values (18, App\Models\Call, 2019-06-22 04:59:56, 2019-06-22 04:59:56))"

How can i do this?

Please sign in or create an account to participate in this conversation.