Reverse Engineering, Larabook

Posted 3 years ago by MarkMatute
<?php namespace Larabook\Users;

trait FollowableTrait {

    /**
     * Get the list of users that the current user follows.
     *
     * @return mixed
     */
    public function followedUsers()
    {
        return $this->belongsToMany(static::class, 'follows', 'follower_id', 'followed_id')->withTimestamps();
    }

    /**
     * Get the list of users who follow the current user.
     *
     * @return mixed
     */
    public function followers()
    {
            return $this->belongsToMany(static::class, 'follows', 'followed_id', 'follower_id')->withTimestamps();
        }

    /**
     * Determine if current user follows another user.
     *
     * @param User $otherUser
     * @return bool
     */
    public function isFollowedBy(User $otherUser)
    {
        $idsWhoOtherUserFollows = $otherUser->followedUsers()->lists('followed_id');

        return in_array($this->id, $idsWhoOtherUserFollows);
    }

}

Why is there no hasMany, and there are 2 belongToMany?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.