Laravel: matches array (Tinder clone) to my user returns empty

  laravel, php

I’m making a Tinder clone to practice, I want to retrieve a user matches (when both users like eachother) but when I do:

$user = User::with('matches')->findOrFail(1);

The matches array returns empty, despite all users liking eachothers,

I created a Users_Users_liked pivot table:

Schema::create('users_users_liked', function (Blueprint $table) {

I set up the User model with relationship as follows (notice matches realtionship, which always returns an empty array)

public function likesToUsers()
        return $this->belongsToMany(self::class, 'Users_Users_liked', 'user_id', 'user_liked_id');

    public function likesFromUsers()
        return $this->belongsToMany(self::class, 'Users_Users_liked', 'user_liked_id', 'user_id');

    public function matches()
        return $this->likesFromUsers()->whereIn('user_id', $this->likesToUsers->keyBy('id'));

In my UsersSeeder I give likes to user 1 from all users

        $users = json_decode($json, true);

        foreach ($users as $data)
            $user = new User();
            $user->name = $data['name'];

            if($user->id > 1)


        $user = User::findOrFail(1);

Source: Laravel

Leave a Reply