Package Data | |
---|---|
Maintainer Username: | overtrue |
Maintainer Contact: | anzhengchao@gmail.com (overtrue) |
Package Create Date: | 2016-11-11 |
Package Last Update: | 2024-03-13 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-11 15:07:39 |
Package Statistics | |
---|---|
Total Downloads: | 339,774 |
Monthly Downloads: | 4,019 |
Daily Downloads: | 153 |
Total Stars: | 1,190 |
Total Watchers: | 24 |
Total Forks: | 133 |
Total Open Issues: | 0 |
You can install the package using composer
$ composer require overtrue/laravel-follow -vvv
Then add the service provider to config/app.php
Overtrue\LaravelFollow\FollowServiceProvider::class
Publish the migrations file:
$ php artisan vendor:publish --provider="Overtrue\LaravelFollow\FollowServiceProvider" --tag="migrations"
As optional if you want to modify the default configuration, you can publish the configuration file:
$ php artisan vendor:publish --provider="Overtrue\LaravelFollow\FollowServiceProvider" --tag="config"
And create tables:
$ php artisan migrate
Finally, add feature trait into User model:
use Overtrue\LaravelFollow\Traits\CanFollow;
use Overtrue\LaravelFollow\Traits\CanBeFollowed;
class User extends Model
{
use CanFollow, CanBeFollowed;
}
Add CanXXX
Traits to User model.
use Overtrue\LaravelFollow\Traits\CanFollow;
use Overtrue\LaravelFollow\Traits\CanLike;
use Overtrue\LaravelFollow\Traits\CanFavorite;
use Overtrue\LaravelFollow\Traits\CanSubscribe;
use Overtrue\LaravelFollow\Traits\CanVote;
use Overtrue\LaravelFollow\Traits\CanBookmark;
class User extends Model
{
use CanFollow, CanBookmark, CanLike, CanFavorite, CanSubscribe, CanVote;
}
Add CanBeXXX
Trait to target model, such as 'Post' or 'Music' ...:
use Overtrue\LaravelFollow\Traits\CanBeLiked;
use Overtrue\LaravelFollow\Traits\CanBeFavorited;
use Overtrue\LaravelFollow\Traits\CanBeVoted;
use Overtrue\LaravelFollow\Traits\CanBeBookmarked;
class Post extends Model
{
use CanBeLiked, CanBeFavorited, CanBeVoted, CanBeBookmarked;
}
All available APIs are listed below.
\Overtrue\LaravelFollow\Traits\CanFollow
$user->follow($targets)
$user->unfollow($targets)
$user->toggleFollow($targets)
$user->followings()->get() // App\User:class
$user->followings(App\Post::class)->get()
$user->areFollowingEachOther($anotherUser);
$user->isFollowing($target)
\Overtrue\LaravelFollow\Traits\CanBeFollowed
$object->followers()->get()
$object->isFollowedBy($user)
\Overtrue\LaravelFollow\Traits\CanBookmark
$user->bookmark($targets)
$user->unbookmark($targets)
$user->toggleBookmark($targets)
$user->hasBookmarked($target)
$user->bookmarks()->get() // App\User:class
$user->bookmarks(App\Post::class)->get()
\Overtrue\LaravelFollow\Traits\CanBeBookmarked
$object->bookmarkers()->get() // or $object->bookmarkers
$object->isBookmarkedBy($user)
\Overtrue\LaravelFollow\Traits\CanLike
$user->like($targets)
$user->unlike($targets)
$user->toggleLike($targets)
$user->hasLiked($target)
$user->likes()->get() // default object: App\User:class
$user->likes(App\Post::class)->get()
\Overtrue\LaravelFollow\Traits\CanBeLiked
$object->likers()->get() // or $object->likers
$object->fans()->get() // or $object->fans
$object->isLikedBy($user)
\Overtrue\LaravelFollow\Traits\CanFavorite
$user->favorite($targets)
$user->unfavorite($targets)
$user->toggleFavorite($targets)
$user->hasFavorited($target)
$user->favorites()->get() // App\User:class
$user->favorites(App\Post::class)->get()
\Overtrue\LaravelFollow\Traits\CanBeFavorited
$object->favoriters()->get() // or $object->favoriters
$object->isFavoritedBy($user)
\Overtrue\LaravelFollow\Traits\CanSubscribe
$user->subscribe($targets)
$user->unsubscribe($targets)
$user->toggleSubscribe($targets)
$user->hasSubscribed($target)
$user->subscriptions()->get() // default object: App\User:class
$user->subscriptions(App\Post::class)->get()
Overtrue\LaravelFollow\Traits\CanBeSubscribed
$object->subscribers() // or $object->subscribers
$object->isSubscribedBy($user)
\Overtrue\LaravelFollow\Traits\CanVote
$user->vote($target) // Vote with 'upvote' for default
$user->upvote($target)
$user->downvote($target)
$user->cancelVote($target)
$user->hasUpvoted($target)
$user->hasDownvoted($target)
$user->votes(App\Post::class)->get()
$user->upvotes(App\Post::class)->get()
$user->downvotes(App\Post::class)->get()
\Overtrue\LaravelFollow\Traits\CanBeVoted
$object->voters()->get()
$object->upvoters()->get()
$object->downvoters()->get()
$object->isVotedBy($user)
$object->isUpvotedBy($user)
$object->isDownvotedBy($user)
All of the above mentioned methods of creating relationships, such as 'follow', 'like', 'unfollow', 'unlike', their syntax is as follows:
follow(array|int|\Illuminate\Database\Eloquent\Model $targets, $class = __CLASS__)
So you can call them like this:
// Id / Id array
$user->follow(1); // targets: 1, $class = App\User
$user->follow(1, App\Post::class); // targets: 1, $class = App\Post
$user->follow([1, 2, 3]); // targets: [1, 2, 3], $class = App\User
// Model
$post = App\Post::find(7);
$user->follow($post); // targets: $post->id, $class = App\Post
// Model array
$posts = App\Post::popular()->get();
$user->follow($posts); // targets: [1, 2, ...], $class = App\Post
$followers = $user->followers
$followers = $user->followers()->where('id', '>', 10)->get()
$followers = $user->followers()->orderByDesc('id')->get()
The other is the same usage.
use Overtrue\LaravelFollow\FollowRelation;
// get most popular object
// all types
$relations = FollowRelation::popular()->get();
// followable_type = App\Post
$relations = FollowRelation::popular(App\Post::class)->get();
// followable_type = App\User
$relations = FollowRelation::popular('user')->get();
// followable_type = App\Post
$relations = FollowRelation::popular('post')->get();
// Pagination
$relations = FollowRelation::popular(App\Post::class)->paginate(15);
Overtrue\LaravelFollow\RelationAttaching
Overtrue\LaravelFollow\RelationAttached
Overtrue\LaravelFollow\RelationDetaching
Overtrue\LaravelFollow\RelationDetached
Overtrue\LaravelFollow\RelationToggling
Overtrue\LaravelFollow\RelationToggled
Event::listen(\Overtrue\LaravelFollow\RelationAttached::class, function($event) {
// $event->causer;
// $event->getTargetsCollection();
// $event->getRelationType();
});
There has a extra properties for Overtrue\LaravelFollow\RelationToggled
event.
$event->results; // ['attached' => [1, 2, 3], 'detached' => [5, 6]]
$event->attached; // [1, 2, 3]
$event->detached; // [5, 6]
想知道如何从零开始构建 PHP 扩展包?
请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》
MIT