Level 102
Try casting it
return (int) $count === 1;
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
I have a many-to-many relationship between two tables.
public function users()
{
return $this->belongsToMany('App\Models\User', 'users_games')
->withPivot('score')
->withTimestamps();
}
I want to test if a user is registered in a game.
So I added this method.
public function isCurrentUserRegistered()
{
$count = $this->withCount(['users' => function (Builder $query)
{
$query->where('user_id', Auth::check() ? Auth::user()->id : null);
}])->get()->first()->users_count;
return $count === 1;
}
The variable $count contains the right values : 0 or 1.
But the test $count === 1 always returns true.
What's the problem with my code ? I don't understand.
Someone has any idea ?
Thanks a lot ;).
Vincent
Or you can try
$id = Auth::check() ? Auth::user()->id : 0;
return $this->users()->where('user_id', $id)->exists();
Please or to participate in this conversation.