Skip to content
Advertisement

Laravel is_follower and is_following implementation like with Instagram

In Instagram we can know am i followed account and is this account followed me and in Laravel i created a followers migration to implementing that.

public function up()
{
    Schema::create('followers', function (Blueprint $table) {
        $table->integer('follower_id')->unsigned();
        $table->integer('following_id')->unsigned();
        $table->primary(['follower_id', 'following_id']);
    });
}

here we can attach or detach user_ids to them for example:

$a->following()->attach($b);

$a->following()->detach($b);

now my question is how can i know a user followed me and a user is on my following list

here i have 2 another model as likes and comments which i want to add is_follower and is_following to them

$user = User::where('...')->with(
    [
        'media.likes.user',
        'media.comments.user',
    ]
)->first();

likes output is:

"likes": [{
  "id": 1,
  "user_id": 1,
  "media_id": 1,
  "created_at": "2022-10-03T07:01:57.000000Z",
  "updated_at": "2022-10-03T07:01:57.000000Z",
  "user": {
    "id": 1,
    "name_family": "user",
    "mobile_number": "0000",
    "active": 1,
    "deleted_at": null
  }
}],

and it should be something like with this output:

"likes": [{
    "id": 1,
  "user_id": 1,
  "media_id": 1,
  "created_at": "2022-10-03T07:01:57.000000Z",
  "updated_at": "2022-10-03T07:01:57.000000Z",
  "user": {
        "id": 1,
    "name_family": "user",
    "mobile_number": "0000",
    "active": 1,
        
    // see this part of json
    "is_follower":true,
    "is_following":false,
        
    "deleted_at": null
  }
}],

how can i implementing this part?

my models:

User

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, SoftDeletes;

    protected $hidden = [
        'password',
        'remember_token',
        'created_at',
        'updated_at'
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function account(): HasMany
    {
        return $this->hasMany(Account::class);
    }


    public function media(): HasMany
    {
        return $this->hasMany(Media::class);
    }

    public function followings(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'followers', 'follower_id', 'following_id');
    }

    public function followers(): BelongsToMany
    {
        return $this->belongsToMany(User::class, 'followers', 'following_id', 'follower_id');
    }

    public function highlight_category(): HasMany
    {
        return $this->hasMany(HighLightCategory::class);
    }
}

Media:

class Media extends Model
{
    use HasFactory;

    public function likes(): HasMany
    {
        return $this->hasMany(Liker::class);
    }

    public function comments(): HasMany
    {
        return $this->hasMany(Comment::class);
    }
}

Likers:

class Liker extends Model
{
    use HasFactory;

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }
}

Comments:

class Comment extends Model
{
    use HasFactory,SoftDeletes;

    protected $fillable = [];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function replies(): HasMany
    {
        return $this->hasMany(Comments::class, 'parent_id');
    }
}

Advertisement

Answer

Instead of a Boolean you can accomplish this with the help of withCount() method:

$user = User::where('...')->with(
    [
        'media.likes.user' => fn($q) => $q->withCount([ 'followings', 'followers']),
        'media.comments.user' => fn($q) => $q->withCount([ 'followings', 'followers']),
    ]
)->first();

Now,

// For likes
$is_following = $user->media->likes->user->followings_count > 0; 
$is_follower = $user->media->likes->user->followers_count > 0;

// For comments
$is_following = $user->media->comments->user->followings_count > 0;
$is_follower = $user->media->comments->user->followers_count > 0;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement