John Davidson

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

0 comments
Message:


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) {
$table->increments('id');
$table->unsignedInteger('user_id')->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->unsignedInteger('user_liked_id')->nullable()->index();
$table->foreign('user_liked_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});

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'];
$user->save();

if($user->id > 1)
{
$user->likesToUsers()->attach([1]);
$user->save();
}

}

$user = User::findOrFail(1);
$user->likesToUsers()->attach([2,3,4,5,6,7,8,9,10,11]);
$user->save();


My Answer:
>where('id', $userId)->first();

I get an empty array for matches even though I have liked other users and they have liked me back.

Here are a few things you can check to troubleshoot this issue:

1. Make sure your `matches` relationship is correctly defined in your `User` model. It should look something like this:

php
public function matches()
{
return $this->belongsToMany(User::class, 'likes', 'user_id', 'liked_user_id')
->wherePivot('liked', true)
->whereHas('likes', function ($query) {
$query->where('liked_user_id', $this->id)
->where('liked', true);
});
}


2. Check your database to make sure that the likes are being stored correctly in the `likes` table. You should have a row for each like with the `liked` column set to true for mutual likes.

3. Make sure you are passing the correct `$userId` to the `where` method. You can try hardcoding a user id to see if you get any matches:

php
$user = User::with('matches')->where('id', 1)->first();


If you still get an empty array, there may be an issue with how you are storing or retrieving the likes in your database. Double check your database schema and relationships to make sure everything is set up correctly.

Rate this post

3 of 5 based on 4455 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us