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

jeroenvanrensen's avatar

Tinker - PHP Error: Call to a member function where() on null in C:/wamp64/www/laravel/tweety/app/Tweet.php on line 32

Hello everyone,

I'm new to laravel and I'm following this tutorial where I'm making a clone of Twitter. I'm making a like/dislike system for tweets, and I'm using this code for a function:

public function isLikedBy(User $user)
{
    return (bool) $user->likes
        ->where('tweet_id', $this->id)
        ->where('liked', true)
        ->count();
}

If I run it in Tinker, this is my code:

$user = App\User::first(); // works fine
$tweet = App\Tweet::first(); // works also fine
$tweet->like($user); // is also working
$tweet->isLikedBy($user); // I got the following error

And this is my error:

PHP Error:  Call to a member function where() on null in C:/wamp64/www/laravel/tweety/app/Tweet.php on line 32

I guess it has something to do with $user->likes, but I'm not sure. I've dubble dubble dubble checked if I made an type mistake, but my code is the same as the code from the teacher.

Does anyone know what to do now?

If you need more information, please ask me.

Thank you! Jeroen

0 likes
3 replies
tykus's avatar
tykus
Best Answer
Level 104

Do you have a likes relationship on the User model? It appears that you may not have that implemented and $user->likes is returning null

Once you have the relation implemented, you could use the Eloquent Builder to achieve the isLikedBy method instead of the Collection method:

public function isLikedBy(User $user)
{
    return $user->likes()
        ->where('tweet_id', $this->id)
        ->where('liked', true)
        ->exists();
}
1 like
buzzyburrows's avatar

Simple test - fetch a user's likes directly in tinker and see if it works:

$user = App\User::first()

$user->likes

Please or to participate in this conversation.