Laravel API JSON定制和表关系
我正在使用Laravel开发一个社交媒体项目。我需要为追随者和追随者提供一个定制的API。因此,我想在用户表和关注者表之间建立一种关系。我查看了很多资源,但感到困惑。有人能帮我处理人际关系吗 型号 用户:id、用户名、电子邮件、密码 跟随:id,用户id,跟随id(用户id) 追随者:id,用户id,追随者id(用户id) API Url:http://localhost/api/follows/user_id/1 跟随控制器Laravel API JSON定制和表关系,laravel,relationship,laravel-7,laravel-8,laravel-relations,Laravel,Relationship,Laravel 7,Laravel 8,Laravel Relations,我正在使用Laravel开发一个社交媒体项目。我需要为追随者和追随者提供一个定制的API。因此,我想在用户表和关注者表之间建立一种关系。我查看了很多资源,但感到困惑。有人能帮我处理人际关系吗 型号 用户:id、用户名、电子邮件、密码 跟随:id,用户id,跟随id(用户id) 追随者:id,用户id,追随者id(用户id) API Url:http://localhost/api/follows/user_id/1 跟随控制器 公共功能用户跟随($user\u id) { $user\u fin
公共功能用户跟随($user\u id)
{
$user\u find=如下::其中(“user\u id”、“=”、$user\u 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模型中为
用户定义一对多关系,如下所示:
跟随
public function user(){
return $this->belongsTo('App\Models\User');
}
然后,从控制器中的以下位置获取相关用户:
$user_find = Follows::where("user_id", "=", $user_id)->get();
$related_user = $user_find->pluck('user');
从您的模型可以明显看出,在Follow模型中为用户定义一对多关系,如下所示:
跟随
public function user(){
return $this->belongsTo('App\Models\User');
}
然后,从控制器中的以下位置获取相关用户:
$user_find = Follows::where("user_id", "=", $user_id)->get();
$related_user = $user_find->pluck('user');
最适合我的实现如下所示:
首先,跟踪后续操作不需要两个单独的表。有一张桌子,两样都可以
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方法的另一种方法是使用基本模型类的“hasManyThrough”方法
public function followers()
{
return $this->hasManyThrough(UserFollower::class, User::class);
}
这样,此方法将返回用户模型最适合我的实现如下所示:
首先,跟踪后续操作不需要两个单独的表。有一张桌子,两样都可以
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方法的另一种方法是使用基本模型类的“hasManyThrough”方法
public function followers()
{
return $this->hasManyThrough(UserFollower::class, User::class);
}
通过这种方式,此方法返回用户模型请提供有关输出的信息。你想要什么?告诉我们您的控制器。我想从输出中有额外follow_id的用户的用户表中获取信息。请使用larave资源和集合进行api响应,否则您需要从表和关系中创建数组,这是一个痛苦的过程。请提供有关输出的信息。你想要什么?告诉我们您的控制器。我想从输出中有额外follow_id的用户的用户表中获取信息。使用larave资源和集合进行api响应,否则您需要从表和关系中创建数组,这是一个痛苦的过程。非常感谢@alihan.dev您很高兴谢谢@alihan.dev你很好,非常感谢