Skip to content

Instantly share code, notes, and snippets.

@inxilpro
Created May 16, 2020 00:28
Show Gist options
  • Save inxilpro/629ed61eb941ef36bc9e911db6e433f1 to your computer and use it in GitHub Desktop.
Save inxilpro/629ed61eb941ef36bc9e911db6e433f1 to your computer and use it in GitHub Desktop.
<?php
class User extends Model
{
public function avatar()
{
return $this->belongsTo(Avatar::class);
}
}
class Avatar extends Model
{
public function uploader()
{
return $this->bellongsTo(User::class);
}
}
$user = new User();
$avatar = new Avatar();
$avatar->uploader()->associate($user);
$user->avatar()->associate($avatar);
$user->toArray(); // Maximum function nesting level of '256' reached
$user->getQueueableRelations(); // Maximum function nesting level of '256' reached
$user->toSearchableArray(); // Maximum function nesting level of '256' reached
@reinink
Copy link

reinink commented May 16, 2020

So, as noted on Twitter, I wonder if you can solve this by simply breaking the variable reference in PHP. Something like this:

<?php

$user = new User();
$avatar = new Avatar();

$avatar->uploader()->associate($user);
$user->avatar()->associate($avatar->withoutRelations());

Calling withoutRelations() will clone the model and unset all the relationships.

<?php

/**
 * Duplicate the instance and unset all the loaded relations.
 *
 * @return $this
 */
public function withoutRelations()
{
    $model = clone $this;

    return $model->unsetRelations();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment