Having trouble creating a Like system for comments in a Laravel Project

xslibx

I'm creating a project using Laravel. Users are able to like comments. I want to display a "like" button so a user can like a comment and if the user has already liked the comment I want that button to be "unlike" so a user can unlike the liked comment

In my database I have a likes table:

| id | user_id | comment_id |

My Like Model looks like this:

class Like extends \Eloquent {

    protected $fillable = ['user_id', 'comment_id'];
    protected $table = 'likes';

    public function owner()
    {
        return $this->belongsTo('Acme\Users\User', 'user_id');
    }
}

Comment Model looks like this:

class Comment extends \Eloquent {

    protected $fillable = ['user_id', 'post_id', 'body'];
    protected $table = 'comments';

    public function owner()
    {
        return $this->belongsTo('Acme\Users\User', 'user_id');
    }
    public function likes()
    {
        return $this->hasMany('Acme\Likes\Like');
    }
}

User model:

class User extends Eloquent {

    public function comments()
    {
        return $this->hasMany('Acme\Comments\Comment');
    }
    public function likes()
    {
        return $this->hasMany('Acme\Likes\Like');
    }

}

Likes controller :

class LikesController extends \BaseController {

use CommanderTrait;

/**
 * Like a comment
 * @return Response
 */
public function commentLike()
{
// using a command bus. Basically making a post to the likes table assigning user_id and comment_id then redirect back
    extract(Input::only('user_id', 'comment_id'));
    $this->execute(new CommentLikeCommand($user_id, $comment_id));

    return Redirect::back();
}
public function unlike()
{
    $like = new Like;
    $user = Auth::user();
    $id = Input::only('comment_id');
    $like->where('user_id', $user->id)->where('comment_id', $id)->first()->delete();
    return Redirect::back();
}
}

In my view I'm able to get the comments via $comment, and I'm able to get likes via $comment->like have:

@foreach($post->comments as $comment)
<div class="user-comment">
    <p class="comment">
        {{ $comment->owner->first_name }}&nbsp;{{ $comment->owner->last_name }}&nbsp;{{ $comment->body }}
    </p>

    <div class="com-details">
<!-- how long ago the comment was posted -->
    <div class="com-time-container">
        &nbsp;{{ $comment->created_at->diffForHumans() }} ·
    </div>

<!-- HERE IS WHERE I WANT THE LIKE AND UNLIKE BUTTONS TO DISPLAY -->
    @if ($comment->likes->owner->id === $currentUser->id)
        {{ Form::open(['route' => 'like']) }}
            {{ Form::hidden('user_id', $currentUser->id) }}
            {{ Form::hidden('comment_id', $comment->id) }}
            <button type="submit" class="com-like">Like</button>
        {{ Form::close() }}
    @else
        {{ Form::open(['route' => 'unlike']) }}
            {{ Form::hidden('user_id', $currentUser->id) }}
            {{ Form::hidden('comment_id', $comment->id) }}
            <button type="submit" class="com-like">Unlike</button>
        {{ Form::close() }}
    @endif
<!-- how many users like this comment -->
    <span class="likes"> · {{ $comment->likes->count() }}</span>
    </div>
</div><!--user-comment end-->
@endforeach

Im trying to set up an if statement to see if the current user has liked the status but im not sure how this is done? If the user has not liked the comment yet I want the "like" button to display. If the user has liked the comment I want the "unlike" button to display. I thought I could say @if($comment->likes->owner->id === $currentUser->id) but I get Undefined property. How would I go about doing this?

patricus

$comment->likes is a Collection of Like objects. To access the owner property, you would need to iterate the collection.

However, another option is to use the available methods on the Collection to do what you need:

@if (in_array($currentUser->id, $comment->likes->lists('user_id')))

$comment->likes->lists('user_id') will return an array of all the user_id values in the Collection of Likes for the Comment. in_array() will check if the $currentUser->id is in that array.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Having trouble creating a Laravel query

From Dev

Having trouble creating a Laravel query

From Dev

Having trouble using LIKE %...%

From Dev

Having trouble creating a tooltip for a button?

From Dev

Having trouble creating makefile for JAVA

From Dev

Laravel API having trouble logging in

From Dev

Clojure UUID - Having trouble creating IDs for defrecords

From Dev

Having trouble creating my own HTML Helper

From Dev

Having trouble dynamically creating buttons in Android Studio

From Dev

Having trouble creating join looking for specific data

From Dev

Having trouble creating join looking for specific data

From Dev

Having some trouble creating a bootable usb

From Dev

Having trouble dynamically creating buttons in Android Studio

From Dev

Clojure UUID - Having trouble creating IDs for defrecords

From Dev

SQL Query having trouble creating temporary table

From Dev

having trouble with design in codeigniter project, using inheritance

From Dev

Having trouble adding currency symbol in my project?

From Dev

Having trouble with implementing a system that detects if an input is a integer or not

From Dev

Having trouble turning mbpfan into a system service

From Dev

Having trouble testing model validation with Laravel

From Dev

Laravel having trouble displaying uploaded images

From Dev

Having trouble validating on update (Laravel 5)

From Dev

Laravel PHP: Having trouble using nest()

From Dev

docker: having trouble running npm install after creating a new user

From Dev

Having trouble creating Gradle flavors for different manifests in a single app

From Dev

Having trouble creating arbitrary new Object, parametrized for function call

From Dev

Creating odds calculator, having trouble with math.random

From Dev

Creating a simple IRC bot in python. Having trouble

From Dev

Having trouble in creating 2D array/list

Related Related

  1. 1

    Having trouble creating a Laravel query

  2. 2

    Having trouble creating a Laravel query

  3. 3

    Having trouble using LIKE %...%

  4. 4

    Having trouble creating a tooltip for a button?

  5. 5

    Having trouble creating makefile for JAVA

  6. 6

    Laravel API having trouble logging in

  7. 7

    Clojure UUID - Having trouble creating IDs for defrecords

  8. 8

    Having trouble creating my own HTML Helper

  9. 9

    Having trouble dynamically creating buttons in Android Studio

  10. 10

    Having trouble creating join looking for specific data

  11. 11

    Having trouble creating join looking for specific data

  12. 12

    Having some trouble creating a bootable usb

  13. 13

    Having trouble dynamically creating buttons in Android Studio

  14. 14

    Clojure UUID - Having trouble creating IDs for defrecords

  15. 15

    SQL Query having trouble creating temporary table

  16. 16

    having trouble with design in codeigniter project, using inheritance

  17. 17

    Having trouble adding currency symbol in my project?

  18. 18

    Having trouble with implementing a system that detects if an input is a integer or not

  19. 19

    Having trouble turning mbpfan into a system service

  20. 20

    Having trouble testing model validation with Laravel

  21. 21

    Laravel having trouble displaying uploaded images

  22. 22

    Having trouble validating on update (Laravel 5)

  23. 23

    Laravel PHP: Having trouble using nest()

  24. 24

    docker: having trouble running npm install after creating a new user

  25. 25

    Having trouble creating Gradle flavors for different manifests in a single app

  26. 26

    Having trouble creating arbitrary new Object, parametrized for function call

  27. 27

    Creating odds calculator, having trouble with math.random

  28. 28

    Creating a simple IRC bot in python. Having trouble

  29. 29

    Having trouble in creating 2D array/list

HotTag

Archive