Search code examples
laravel-5.4table-relationships

Setting up relationships for like system in Laravel 5.4


I was making a simple project to practice with Laravel, but I'm stuck. I can't figure out why anytime I click on the like button, a new row in my table is created... so I have "multiple rows (1)" for any click to the like button I make.

multiple rows (1)

I suspect this has something to do with relationships between my models, but I can't see what I'm making wrong.

My User.php Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;

class User extends \Eloquent implements Authenticatable
{
    use AuthenticableTrait;

    public function likes()
    {
        return $this->hasMany('App\Like');
    }
}

My Memo.php Model

<?php

namespace App;
use Illuminate\Database\Eloquent\Model;

class Memo extends Model
{
    public function likes()
    {
        return $this->hasMany('App\Like');
    }
}

My Like.php Model

<?php

namespace App;
use Illuminate\Database\Eloquent\Model;

class Like extends Model
{
    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function memo()
    {
        return $this->belongsTo('App\Memo');
    }
}

My MemoController method where i handle my AJAX request:

public function likeMemo(Request $request)
{
    $memo_ID = $request['id'];
    $memo = Memo::findOrFail($memo_ID)->first();

    // Is already liked...?
    $like = Auth::user()->likes()->where('memo_id', $memo_ID)->first();

    if($like->is_like) {
        $like->is_like = false;
        $like->update();
        return response()->json(['status' => $like], 200);
    }

    // Insert data into DB.
    else {
        $like = new Like();
        $like->user_id = Auth::user()->id;
        $like->memo_id = $memo_ID;
        $like->is_like = true;
        $like->save();
        return response()->json(['status' => 'inserted like']);
    }

}

The code, should be able to toggle the like in a specific memo, like Instagram since my goal is to make the logic to add those memos in a specific Favourites category.


Solution

  • My guess is that you should update your data if its not liked, and create a new one only if you couldn't find record.

     public function likeMemo(Request $request)
    {
        $memo_ID = $request['id'];
        $memo = Memo::findOrFail($memo_ID)->first();
    
        // Is already liked...?
        $like = Auth::user()->likes()->where('memo_id', $memo_ID)->first();
    
         // Insert data into DB.
        if(!$like) {
            $like = new Like();
            $like->user_id = Auth::user()->id;
            $like->memo_id = $memo_ID;
            $like->is_like = true;
            $like->save();
            return response()->json(['status' => 'inserted like']);
        } else {
    
           if($like->is_like) {
               $like->is_like = false;
               $like->update();
               return response()->json(['status' => $like], 200);
           } else {
               $like->is_like = true;
               $like->update();
               return response()->json(['status' => $like], 200);
          }  
        }
    }