Simplify your concerns and comments - Laravel Follow & Vote

Keywords: PHP Laravel github

This article mainly introduces two packages:

Laravel - Follow : https://github.com/overtrue/laravel-follow

Laravel - Vote : https://github.com/jcc/laravel-vote

You wonder what these two bags are for? Of course, as the title says, simplify your focus and compliment functions, Laravel - Follow for focus functions, Laravel - Vote for compliment functions.

If you look at the code, you will find that the code of the two packages is basically the same. It should be noted that the application scenarios of the two packages are different.

Laravel - Follow Previous versions only support user concerns, of course, some modifications have been done recently to implement applications in a variety of scenarios, such as column concerns, user concerns and other scenarios.

Of course, Laravel - Vote It also supports the application of various scenarios, such as article comment, comment response comment, etc. Not much nonsense. Let me briefly introduce the usage of the two bags.

install

Laravel Follow

Use the composer installation package:

composer require overtrue/laravel-follow -vvv

Add a service provider to config/app.php:

Overtrue\LaravelFollow\FollowServiceProvider::class

Publish migration files:

php artisan vendor:publish --provider="Overtrue\LaravelFollow\FollowServiceProvider" --tag="migrations"

Laravel Vote

Use the composer installation package:

composer require jcc/laravel-vote -vvv

Add a service provider to config/app.php:

Jcc\LaravelVote\VoteServiceProvider::class

Publish migration files:

php artisan vendor:publish --provider="Jcc\LaravelVote\VoteServiceProvider" --tag="migrations"

usage

Of course, the usage is very simple. You don't have to worry about the relationship between them.

Laravel Follow

Application Scenario: Users Focus on Users

First, FollowTrait and FollowrTrait are introduced into the User model.

use Overtrue\LaravelFollow\FollowTrait;
use Overtrue\LaravelFollow\FollowerTrait;

class User extends Model
{
    use FollowTrait, FollowerTrait;
}

Focus on users:

$user->follow(1);

// or

$user->follow([1,2,3,4]);

// or

$target = User::find(2);

$user->follow($target);

Remove user concerns:

$user->unfollow(1);

// or

$user->unfollow([1,2,3,4]);

// or

$target = User::find(2);

$user->unfollow($target);

Get the user's followers:

$user->followers();

Users who get users'attention:

$user->followings();

Determine whether the user is concerned:

$user->isFollowing(1);

// or

$target = User::find(1);

$user->isFollowing($target);

Determine whether the user is being noticed:

$user->isFollowedBy(1);

Laravel Vote

Application Scenario: User Comments

Just introduce VoteTrait in User model and VoterTrait in Post model.

User.php

use Jcc\LaravelVote\VoteTrait;

class User extends Model
{
    use VoteTrait;
}

Post.php

use Jcc\LaravelVote\VoterTrait;

class Post extends Model
{
    use VoterTrait;
}

Operations for User.php

Users praise articles:

$post = Post::find(1);

$user->upVote($post);

Users cancel the comment:

$post = Post::find(1);

$user->downVote($post);

Obtain the user's praise record:

$user->votedItems();

Judging whether or not it has been praised:

$post = Post::find(1);

$user->hasVoted($post);

Operations for articles

Get all the compliments from users:

$post->voters();

Determine if the user is a little appreciative:

$post->isVotedBy(1);

At this point, you can use these two packages happily, of course, you need to use them according to the application scenario you need, rather than using them indiscriminately (Follow as Vote, etc.). What's more, Trait is used here. How to understand and better use Trait-By Overtrue

Finally, of course, I want to thank Superbrother. overtrue .

More attention PJ Blog

Posted by tr0gd0rr on Thu, 20 Dec 2018 02:36:05 -0800