承接 nr-type/like-dislike 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nr-type/like-dislike

Composer 安装命令:

composer require nr-type/like-dislike

包简介

This is a package for developers who want to use like and dislike options in their applications, this is the easiest way to do that.

README 文档

README

Issues Stars Forks Tags License Twitter

About Package

This is a package for developers who want to use like and dislike options in their applications, this is the easiest way to do that.

Installing

This package can be installed through Composer in your application :

Run the command.
composer require nr-type/like-dislike

Migration

After that run the Migration command :

php artisan migrate

Model Use

The model where you want to have like and dislike. You just need to use NrType\LikeDislike\Traits\Likeable and NrType\LikeDislike\Traits\Dislikeable. And inside the class use Likeable and Dislikeable.

use NrType\LikeDislike\Traits\Likeable;
use NrType\LikeDislike\Traits\Dislikeable;

class Post extends Model
{
    use Likeable, Dislikeable;
}
You can use like dislike options in multiple Model

Multiple Model are can be Comment,Video,Photo.

use NrType\LikeDislike\Traits\Likeable;
use NrType\LikeDislike\Traits\Dislikeable;

class Comment extends Model
{
    use Likeable, Dislikeable;
}

Optional

Suppose you just only need Like option for your application. Then you use NrType\LikeDislike\Traits\Likeable and Likeable.

use NrType\LikeDislike\Traits\Likeable;

class Post extends Model
{
    use Likeable;
}

Compatibility

Like and Dislike can work individually and also both are compatible to work with each other, suppose someone like the post and after some time later thinks that he/she wants to dislike the post. He/she just simply click the dislike button, and it will remove his/her like on that particular post and dislike.

Uses in Controller

Like

Use like option in the post such as like 2 lines of code.

public function like (Post $post)
{
    $post->like();

    return redirect()->route('posts.index');
}

But

Suppose you want that someone's click like button then it will like the post and again he click liked button then it will unlike the post. Then here is removeLike(). if User alreay like that post it will remove that particular like in the same method or if you want you can create another route and method to unlike the post.

public function like (Post $post)
{
    if($post->removeLike()){
        return redirect()->route('posts.index');
    }

    $post->like();

    return redirect()->route('posts.index');
}

DisLike

Use dislike in the post same as like in post. For dislike options again you have dislike() and removeDislike()

public function dislike (Post $post)
{
    $post->dislike();

    return redirect()->route('posts.index');
}

Or

public function dislike (Post $post)
{
    if ($post->removeDislike()) {
        return redirect()->route('posts.index');
    }

    $post->dislike();

    return redirect()->route('posts.index');
}

Likers and Dislikers on the Post

You can easily access likers and dislikers through likers() and dislikers(). default it will return with users id,name from users table.

public function likers (Post $post)
{
    return $post->likers(); 
}

Or

public function dislikers (Post $post)
{
    return $post->dislikers(); 
}

Now here has somethings

likers() and dislikers() default return id,name but you maybe don't need id, you may only need name or your users database table doesn't have name field, it can be first_name,last_name or maybe something else or maybe you need more then those fields. Here have a solution. You just pass a array in side the method. It will return those fields data form users table.

public function likers (Post $post)
{
    $fields = ['id','first_name','last_name','age'];

    return $post->likers($fields); 
}

Or

public function dislikers (Post $post)
{
    $fields = ['id','name','age'];

    return $post->dislikers($fields); 
}

with() relationship call in Controller

You can show likes, dislikes on posts and also want to show how many likes and dislike on posts with likeCounter, dislikeCounter.

public function index()
{
    $relations = ['likes','likeCounter','dislikes','dislikeCounter'];

    $data['posts'] = Post::with($relations)->get();

    return view('post.index', $data);
}

Or

You may have comments on posts and you need comments and also need likes on particular comment and how many likes on comment. Then you can use comments.likes and comments.likeCounter.

public function index()
{
    $relations = [
        'likes',
        'likeCounter',
        'dislikes',
        'dislikeCounter',
        'comments.likes',
        'comments.likeCounter'
    ];

    $data['posts'] = Post::with($relations)->get();

    return view('post.index', $data);
}

Additional options

hasLike(), hasDislike() is for check that logged in user has like or dislike in post or comment. It return boolean. You can use hasLike() hasDislike() in blades file as well.

{{ $post->hasLike() ? 'Liked' : 'Like' }}
{{ $post->hasDislike() ? 'Disliked' : 'Dislike' }}

License

Like Dislike bucket is open-sourced software licensed under the MIT license.

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固