【发布时间】:2021-03-30 02:14:27
【问题描述】:
我正在为类似的社交媒体开发一个 API。这个应用程序有一个关注取消关注系统。我做了我的关注者列表和关注者列表查询和响应。但是,我想添加关注信息(布尔值)作为回应。如何解决清除这个问题? (对不起我的英语不好)
我想要这个回复:
"data": [
{
"user_id": 3,
"username": "example",
"name": "Example",
"photo": "default.jpg",
"is_following": true
}
]
目前的回应:
"data": [
{
"user_id": 3,
"username": "example",
"name": "Example",
"photo": "default.jpg",
}
]
跟随关系的用户模型:
function follows(){
return $this->belongsToMany(self::class,"follows","followed_id","following_id")->select("users.id","username","name","photo");
}
function followers(){
return $this->belongsToMany(self::class,"follows","following_id","followed_id")->select("users.id","username","name","photo");
}
我的资源:
public function toArray($request)
{
return [
"user_id" => $this->id,
"username" => $this->username,
"name" => $this->name,
"photo" => $this->photo(),
"is_following" =>
];
}
【问题讨论】:
-
is_following究竟是什么意思?这是否意味着列出的用户正在关注经过身份验证的用户?你的控制器代码是什么?您可以在资源中通过向数组添加额外数据来解决此问题。目前没有添加任何内容...
标签: php laravel eloquent relationship