Laravel的is_follower和is_following实现类似于Instagram



Instagram中,我们可以知道我是跟随帐户的吗?这个帐户是跟随我的吗?在Laravel中,我创建了一个followers迁移来实现它。

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']);
});
}

在这里,我们可以将user_id连接或分离到它们,例如:

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

现在我的问题是,我如何知道有用户关注我,并且有用户在我的following列表上

这里我有另外两个模型likescomments,我想把is_followeris_following添加到中

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

likes输出为:

"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
}
}],

它应该和这个输出类似:

"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
}
}],

我该如何实现这一部分?

我的型号:

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

您可以在withCount()方法的帮助下实现这一点,而不是使用布尔值:

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

现在,

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

相关内容

最新更新