Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

tp5 关联查询问题

表 User
id name
表 Order
id user_id
表 Goods
id order_id name

获取所有数据
1. $this->with(['User','Goods'])->limit($pages,$get['limit'])->select();

得到数据

[
  {
    "id": 1,
    "username": "用户A",
    "Goods": [
      {
        "id": 1,
        "name": "产品1"
      },
      {
        "id": 2,
        "name": "产品2"
      },
      {
        "id": 3,
        "name": "产品3"
      }
    ]
  },
  {
    "id": 2,
    "username": "用户A",
    "Goods": [
      {
        "id": 4,
        "name": "产品4"
      },
      {
        "id": 2,
        "name": "产品2"
      }
    ]
  },
  {
    "id": 3,
    "username": "用户B",
    "Goods": [
      {
        "id": 1,
        "name": "产品1"
      },
      {
        "id": 7,
        "name": "产品7"
      }
    ]
  }
]

现在想关联查询获得如下数据,不知道怎么操作

[
  {
    "id": 1,
    "username": "用户A",
    "Goods": [
      {
        "id": 2,
        "name": "产品2"
      }
    ]
  },
  {
    "id": 2,
    "username": "用户A",
    "Goods": [
      {
        "id": 2,
        "name": "产品2"
      }
    ]
  }
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

额.我是不是可以理解为,你就是想查产品2关联了哪些用户啊?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...