Laravel API JSON 自定义和 table 关系
Laravel API JSON customization and table relationship
我正在使用 Laravel 开发社交媒体项目。我需要为 Follows 和 Followers 提供定制的 API。因此,我想在用户 table 和关注者之间建立关系并关注 table。我看过很多资源,但感到困惑。谁能帮我处理人际关系?
型号
用户: ID、用户名、电子邮件、密码
如下: id,user_id,follow_id(user_id)
关注者: id,user_id,follower_id(user_id)
API Url: http:///localhost/api/follows/user_id/1
跟随控制器
public function user_follow($user_id)
{
$user_find = Follows::where("user_id", "=", $user_id)->get();
return response()->json($user_find, 201);
}
输出
[
{
"id": 4,
"user_id": 1,
"follow_id": 4,
"created_at": "2020-03-23T15:11:48.000000Z",
"updated_at": "2020-12-11T11:10:10.000000Z"
},
{
"id": 5,
"user_id": 1,
"follow_id": 1,
"created_at": "2012-08-30T20:16:51.000000Z",
"updated_at": "2020-12-11T11:10:10.000000Z"
}
]
API 根据用户要求带来相关的关注人。但是在这里,我想给相关用户额外的用户信息,我该怎么做呢?
从您的模型中可以明显看出,在 Follow 模型中为 user
定义一对多关系,如下所示:
关注
public function user(){
return $this->belongsTo('App\Models\User');
}
然后,从控制器中的关注中获取相关用户:
$user_find = Follows::where("user_id", "=", $user_id)->get();
$related_user = $user_find->pluck('user');
对我来说最正确的实现是这样的:
首先,您不需要两个单独的 table 来跟踪后续操作。一个 table 你可以同时拥有两个。
users
*id
*username
*email
*password
user_followers
*id
*user_id
*follower_id
然后在模型中你可以这样做:
public function followers()
{
return UserFollower::where('user_id', $this->id)->get();
}
public function following()
{
return UserFollower::where('follower_id', $this->id)->get();
}
public function followUser($userId)
{
// create & insert new row as a follower to the other user
$follower = new UserFollower();
$follower->user_id = $userId;
$follower->follower_id = $this->id;
$follower->save();
}
并且在 API 中你可以做:
domain/api/users/{user_id}/followers/
domain/api/users/{user_id}/following/
• 要从相关模型中获取更多信息,我认为您可以 'leftJoin'。
类似的东西
public function followers()
{
return DB::table('user_followers')
->select('user_followers.*', 'users.email', 'users.name')
->where('user_followers.user_id', '=', $this->id)
->leftJoin('users', 'user_followers.follower_id', '=', 'users.id')
->get();
}
编辑:followers 方法的另一种方法是使用基础模型的 class 的 'hasManyThrough' 方法。
public function followers()
{
return $this->hasManyThrough(UserFollower::class, User::class);
}
这种方式这种方法returns用户模型
我正在使用 Laravel 开发社交媒体项目。我需要为 Follows 和 Followers 提供定制的 API。因此,我想在用户 table 和关注者之间建立关系并关注 table。我看过很多资源,但感到困惑。谁能帮我处理人际关系?
型号
用户: ID、用户名、电子邮件、密码
如下: id,user_id,follow_id(user_id)
关注者: id,user_id,follower_id(user_id)
API Url: http:///localhost/api/follows/user_id/1
跟随控制器
public function user_follow($user_id)
{
$user_find = Follows::where("user_id", "=", $user_id)->get();
return response()->json($user_find, 201);
}
输出
[
{
"id": 4,
"user_id": 1,
"follow_id": 4,
"created_at": "2020-03-23T15:11:48.000000Z",
"updated_at": "2020-12-11T11:10:10.000000Z"
},
{
"id": 5,
"user_id": 1,
"follow_id": 1,
"created_at": "2012-08-30T20:16:51.000000Z",
"updated_at": "2020-12-11T11:10:10.000000Z"
}
]
API 根据用户要求带来相关的关注人。但是在这里,我想给相关用户额外的用户信息,我该怎么做呢?
从您的模型中可以明显看出,在 Follow 模型中为 user
定义一对多关系,如下所示:
关注
public function user(){
return $this->belongsTo('App\Models\User');
}
然后,从控制器中的关注中获取相关用户:
$user_find = Follows::where("user_id", "=", $user_id)->get();
$related_user = $user_find->pluck('user');
对我来说最正确的实现是这样的:
首先,您不需要两个单独的 table 来跟踪后续操作。一个 table 你可以同时拥有两个。
users
*id
*username
*email
*password
user_followers
*id
*user_id
*follower_id
然后在模型中你可以这样做:
public function followers()
{
return UserFollower::where('user_id', $this->id)->get();
}
public function following()
{
return UserFollower::where('follower_id', $this->id)->get();
}
public function followUser($userId)
{
// create & insert new row as a follower to the other user
$follower = new UserFollower();
$follower->user_id = $userId;
$follower->follower_id = $this->id;
$follower->save();
}
并且在 API 中你可以做:
domain/api/users/{user_id}/followers/
domain/api/users/{user_id}/following/
• 要从相关模型中获取更多信息,我认为您可以 'leftJoin'。
类似的东西
public function followers()
{
return DB::table('user_followers')
->select('user_followers.*', 'users.email', 'users.name')
->where('user_followers.user_id', '=', $this->id)
->leftJoin('users', 'user_followers.follower_id', '=', 'users.id')
->get();
}
编辑:followers 方法的另一种方法是使用基础模型的 class 的 'hasManyThrough' 方法。
public function followers()
{
return $this->hasManyThrough(UserFollower::class, User::class);
}
这种方式这种方法returns用户模型