Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbehoM's avatar

Laravel morphs question and error

I'm creating a project where an user can have different profile information

That is the user migration:

        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');

            $table->string('profile_type')->nullable();
            $table->unsignedBigInteger('profile_id')->nullable();

            $table->rememberToken();
            $table->timestamps();
        });

And the advertiser profile (removed the fields for example purposes):

        Schema::create('advertiser_profiles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('description');
            $table->timestamps();
        });

In the User model:

    protected $with = ['profile'];

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

In the AdvertiserProfile model:

    public function user()
    {
        return $this->morphOne(User::class, 'profile');
    }

The question: I need to be able to create the user without a profile so the fields profile_type and profile_id should be nullable. I tried to use $table->morphs('profile') but it creates the fields as not null, when trying to use ->nullable() in the $table->morphs it doesn't work. In the docs doesn't mention how to make $table->morphs nullable.

The error: You might noticed that I used protected $with = ['profile']; in the User model so whenever I retrieve an user I will have the profile attached without the need to call it later. The error happens on the opposite side, if I put protected $with = ['user']; in the AdvertiserProfile with the morphOne I get an error saying:

PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes)

How can I solve this?

0 likes
6 replies
Snapey's avatar

NEVER, EVER have with in both models. User loads Profile, loads User, loads Profile, loads User etc etc until you run out of memory

I did not have any issue with creating the user first, not sure why you are getting this or where you are trying to use $table->morphs('profile') ?

AbehoM's avatar

@snapey You are right, using with in both gives me this loop, thank you for warning.

The $table->morphs('profile') is on the user table migration, it creates the fields as not null (in your tutorial you defined the fields manually and not using $table->morphs that is why it works creating the user before setting the profile)

Snapey's avatar

So why do you need to do it that way and not just copy my example?

Snapey's avatar
Snapey
Best Answer
Level 122

Anyway, again the docs is there;

$table->nullableMorphs('taggable');
1 like
AbehoM's avatar

@SNAPEY - I swear to God I searched for nullableMorphs but I couldn't find, perhaps Google sent me to a different version of the docs and I didn't noticed.

Thank you

devtiagofranca's avatar
// If you morph table id are UUID...

// Change this
$table->morphs('tokenable');

// To this
$table->uuidMorphs('tokenable');
1 like

Please or to participate in this conversation.