nacha's avatar
Level 2

like dislike rating comment in a ecommerce site

how to make like, dislike ,rating and comment in a ecommerce site with simple,easy and clean code or use pakage which one I choose and if there's any code to help me to do it with the right way thank you

0 likes
8 replies
nacha's avatar
Level 2

thank you @martinbean no I'm not tried anything but I'm confused to use package like this

Voting include CanVote trait into your user model to apply rating functionalties



use Nagy\LaravelRating\Traits\Vote\CanVote;

class User extends Model
{
    use CanVote;
include Votable trait to your model that will be votable

use Nagy\LaravelRating\Traits\Vote\Votable;

class Post extends Model
{
    use Votable;
now you can vote your model as the following:

// up vote or +1  your model
$user->upVote($postModel);

// down vote or -1 your model
$user->downVote($postModel);
get total votes count

$postModel->votesCount();
get total up votes count

$postModel->upVotesCount();
get total down votes count

$postModel->downVotesCount();
get the up voted models by a user

$user->upVoted(); // returns a collection of up voted models
get the down voted models by a user

$user->downVoted(); // returns a collection of down voted models
get the total voted models by a user

$user->voted(); // returns a collection of total voted models;

Like & Dislike include CanLike trait into your user model to apply like and dislike functionalties



use Nagy\LaravelRating\Traits\Like\CanLike;

class User extends Model
{
    use CanLike;
include Likeable trait to your model that will be likeable

use Nagy\LaravelRating\Traits\Like\Likeable;

class Post extends Model
{
    use Likeable;
now you can like your model as the following:

// like
$user->like($postModel);

// dislike
$user->dislike($postModel);
get total likes count

$postModel->likesCount();
get total dislikes count

$postModel->dislikesCount();
get total likes and dislikes count

$postModel->likesDislikesCount();
get the liked models by a user

$user->liked(); // return a collection of liked models;
get the disliked models by a user

$user->disliked(); // return a collection of disliked models;
get the total liked and disliked models by a user

$user->likedDisliked(); // return a collection of liked and disliked models;

or use tables for likes ,dislikes and comments

Snapey's avatar

There are many parts to what you want to do.

First thing is the view that is going to capture the user's rating. When you can get that value into the controller then you can use the package to set the rating for the model.

nacha's avatar
Level 2

@snapey thank you very much it's the first time that I will use like,dislike,rating... I will use it for an ecommerce site (like,dislike,rating, comment a product) so you suggest to use a package yes or not ,if yes I will try to use it thank you

channaveer's avatar

You can achieve it in very simple way using Polymorphic relations. For example for Likes Table you can have

id, user_id, likeable_id, likeable_type, created_at, updated_at, deleted_at

Why? Why should I use polymorphic relations? Because think that if you would like to add the Likes feature for different models like Products, Comments or something else. Then you have to create a separate table.

NOTE: Again it depends on your personal or your team preference on which way you guys want to implement.

So once you do with the above table. You can have LikeController which has store() and destory() methods to store and delete the likes. Following is the simple way to achieve the same

public function store ($postId) {
    $post = (new PostService)->getPostById($postId);

    $postLike = $post->commentLikes()->firstOrCreate([
        'user_id' => $this->user()->id
    ]);

    if ($postLike->wasRecentlyCreated) {
        $post->increment('likes_count');
    }

    $post->fresh();

    event(new PostLikedEvent($post, $this->user()));

    //Some other stuffs
}


public function destroy($postId){
    $post = (new PostService)->getPostById($postId);

    $postUnLike = $post->commentLikes()->where(['user_id' => $this->user()->id])->delete();

    if ($postUnLike) {
        $post->decrement('likes_count');
    }

    //Some other stuffs
}

NOTE: I have used postId as parameter instead of Post $post because I wanted to do some cross check in my Service classes but you can do the way you want.

Similarly, you can implement the rating & comment section too.

Please or to participate in this conversation.