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?