定制 infobizzs/laravel-follow 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

infobizzs/laravel-follow

Composer 安装命令:

composer require infobizzs/laravel-follow

包简介

Laravel 5 User Based System

README 文档

README

❤️ This package helps you to add user based follow system to your model.

Build Status Latest Stable Version Latest Unstable Version Build Status Scrutinizer Code Quality Code Coverage Total Downloads License

Features

  • Support actions:
    • Follow
    • Like
    • Bookmark
    • Subscribe
    • Favorite
    • Vote (Upvote & Downvote)

Installation

Required

  • PHP 7.0 +
  • Laravel 5.5 +

You can install the package using composer

$ composer require infobizzs/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;
}

Usage

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.

Follow

\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)

Bookmark

\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)

Like

\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)

Favorite

\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)

Subscribe

\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)

Vote

\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)

Parameters

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

Query relations

$followers = $user->followers
$followers = $user->followers()->where('id', '>', 10)->get()
$followers = $user->followers()->orderByDesc('id')->get()

The other is the same usage.

Working with model.

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); 

Events

  • Overtrue\LaravelFollow\Events\RelationAttaching
  • Overtrue\LaravelFollow\Events\RelationAttached
  • Overtrue\LaravelFollow\Events\RelationDetaching
  • Overtrue\LaravelFollow\Events\RelationDetached
  • Overtrue\LaravelFollow\Events\RelationToggling
  • Overtrue\LaravelFollow\Events\RelationToggled
Event::listen(\Overtrue\LaravelFollow\Events\RelationAttached::class, function($event) {
    // $event->causer; 
    // $event->getTargetsCollection(); 
    // $event->getRelationType();
});

About toggled event.

There has a extra properties for Overtrue\LaravelFollow\Events\RelationToggled event.

$event->results; // ['attached' => [1, 2, 3], 'detached' => [5, 6]]
$event->attached; // [1, 2, 3]
$event->detached; // [5, 6]

PHP 扩展包开发

想知道如何从零开始构建 PHP 扩展包?

请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》

License

MIT

infobizzs/laravel-follow 适用场景与选型建议

infobizzs/laravel-follow 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 02 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 infobizzs/laravel-follow 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 infobizzs/laravel-follow 我们能提供哪些服务?
定制开发 / 二次开发

基于 infobizzs/laravel-follow 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-02-07