I have 2 model: User and Team. - A user belongs to a team. - A team has many users.
And here is my User
model.
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable {
use Notifiable, HasRole;
protected $guarded = [];
protected $with = ['team', 'role'];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function team()
{
return $this->belongsTo(Team::class);
}
}
Team
model:
class Team extends Model {
protected $with = ['leader'];
protected static function boot()
{
parent::boot();
static::addGlobalScope('membersCount', function ($builder) {
$builder->withCount('members');
});
}
public function leader()
{
return $this->belongsTo(User::class, 'leader_id');
}
public function members()
{
return $this->hasMany(User::class);
}
}
When I tried to eager load team
in User model by using protected $with = ['team'];
, it ends up with a error
Maximum function nesting level of '512' reached, aborting!
Can anyone help me? Thank you!