Laravel API JSON定制和表关系 [英] Laravel API JSON customization and table relationship

查看:83
本文介绍了Laravel API JSON定制和表关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel开发一个社交媒体项目.我需要为关注和关注者提供定制的API.因此,我想在用户表与关注者和关注表之间建立关系.我看了很多资源,但感到困惑.有人可以帮助我建立人际关系吗?

I'm developing a social media project using Laravel. I need to provide a customized API for Follows and Followers. Accordingly, I want to establish a relationship between the user table and the followers and followed tables. I have looked at a lot of resources but get confused. Can anyone help me with relationships?

模型

用户: ID,用户名,电子邮件,密码

Users: id, username, email, password

关注:id,user_id,follow_id(user_id)

Follows: id, user_id, follow_id(user_id)

关注者:id、user_id、follower_id(user_id)

Followers: id, user_id, follower_id(user_id)

API网址: http://localhost/api/follows/user_id/1

API Url: http://localhost/api/follows/user_id/1

FollowController

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.但是在这里,我想提供相关用户的其他用户信息,我该怎么做?

API that brings relevant followed people according to user request. But here, I want to give additional user information of the relevant users, how do I do this?

推荐答案

对我来说,最正确的实现是这样的:

The most right implementation for me looks like this:

首先,您不需要两个单独的表来跟踪跟踪操作.一张桌子可以同时拥有.

First you do not need two separate tables for tracking follow actions. With one table you can have both.

users
*id
*username
*email
*password

user_followers
*id
*user_id
*follower_id

然后在模型中您可以执行以下操作:

Then in the model you can do something like:

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中,您可以执行以下操作:

And in the API you can do:

domain/api/users/{user_id}/followers/
domain/api/users/{user_id}/following/

•为了从相关模型中获取更多信息,我认为您可以执行"leftJoin".

• To get additional information from the related model, I think you could do a '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方法的另一种方法是使用基本Model类的'hasManyThrough'方法.

Another approach for the followers method is to use the 'hasManyThrough' method of the base Model's class.

public function followers()
{
    return $this->hasManyThrough(UserFollower::class, User::class);
}

这种方法可以返回用户模型

This way this method returns User models

这篇关于Laravel API JSON定制和表关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆