User follow unfollow system for Yii2.
$ composer require kayw-geek/yii2-follow
This step is also optional, if you want to custom the pivot table, you can publish the migration files:
$ yii migrate/up --migrationPath=@vendor/kayw-geek/yii2-follow/migrations
use kaywGeek\follow\FollowerTrait;
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
class User extends ActiveRecord implements IdentityInterface
{
<...>
use FollowerTrait;
<...>
}
$user1 = User::findOne(1);
$user2 = User::findOne(2);
$user1->follow($user2);
$user1->unfollow($user2);
$user1->checkFollowed($user2);
$user1->followedCount();
$user1->followerCount();
$user->followings;
$user->followers;
// with query where
$user->followings()->where(['<',['follow_at'=>date('Y-m-d')]])->all();
// followers orderBy
$user->followers()->orderBy('follow_at desc');