What to backfill with in type-column on polymorphic table?

Viewed 33

I am changing my File-model to go from one to many to be a one to many polymorphic relation. Before only my User-model could have a file but now my model Guest will also be able to have a file.

So before I had a column in the file table named user_id I have renamed that one following the pattern here: https://laravel.com/docs/7.x/eloquent-relationships#many-to-many-polymorphic-relations

So it is now named fileable_id.

In addition to the renaming I have added a column named fileable_type. I now want to backfill that column so everything existing points on the User-model. What should I write in that column?

I have tried App\User and User without success.

1 Answers

App\User should be correct if that is how you have your User model namespaced. The polymorphich relationships use the getMorphClass() method on the Eloquent model. You can verify this by doing:

$u = new User();
dd($u->getMorphClass());

If you have not created a custom polymorphic type for the User model, then getMorphClass() should be equivalent to User::class.

Related