关联查询指定表的字段

Flight表关联了多张表,如果不指定字段,默认会返回所有,显得找起来很麻烦,尤其是表中很多字段的情况!

$result = Flight::query()
->where(['fltNr' => $id])->with([
    'flightPlayers',
    'flightPlayers.relation',
    'flightPlayers.greenfee',
    'flightPlayers.sales',
    'flightPlayers.sales.item',
    'flightPlayers.sales.salesTransaction',
])->first();
1
2
3
4
5
6
7
8
9

输出结果

{
	"fltNr": 9451976,
	"fltComNr": 9,
	"fltRefType": null,
	"fltRefNr": null,
	"fltDate": 738084,
	"fltTime1Booked": 580,
	"fltTimeFixedYN": null,
	"fltTime1": 580,
	"fltBreakTime": null,
	"fltTime2": 720,
	"fltCrlNr1": 44,
	"fltCrlNr2": 0,
	"fltAloneYN": null,
	"fltHole": null,
	"fltOrigin": null,
	"fltSize": 3,
	"fltCheckedIn": 0,
	"fltTimestamp": "2020-08-20 15:51:37",
	"fltCarNr": null,
	"fltNotes": null,
	"fltDownPaymentPerc": null,
	"fltPaid": null,
	"fltAgtNr": null,
	"fltHotNr": null,
	"fltOptionDate": null,
	"fltCcard1": null,
	"fltCcard2": null,
	"fltCcardExpMonth": null,
	"fltCcardExpYear": null,
	"fltCcardCode": null,
	"fltCcard3": null,
	"fltCcard4": null,
	"fltResNr": null,
	"fltCost": null,
	"fltStatus": null,
	"fltHosNrLocked": 94,
	"fltCheckResult": null,
	"fltHosNr": 94,
	"fltEmpNr": 24,
	"flight_players": [{
		"flpNr": 29803563,
		"flpFltNr": 9451976,
		"flpMatch": null,
		"flpSide": 1,
		"flpRelNr": 309,
		"flpRelNrWildcard": null,
		"flpName": "Slof",
		"flpGrpNr1": null,
		"flpGrpNr2": null,
		"flpGrpNr3": null,
		"flpExtra": null,
		"flpPhone": "0172617000",
		"flpEmail": "595@mms.nl",
		"flpHandicap": 25.1,
		"flpGrfNr": 7,
		"flpGfcNr": null,
		"flpItmNr": 212,
		"flpPrice": 25,
		"flpBilNr": null,
		"flpGrfNrDiscount": null,
		"flpGfcNrDiscount": null,
		"flpItmNrDiscount": null,
		"flpDiscount": null,
		"flpSalNrDiscount": null,
		"flpVoucher": null,
		"flpScorecard": null,
		"flpQualifying": null,
		"flpCarNr": null,
		"flpIntro": null,
		"flpTeebox": null,
		"flpCheckResult": null,
		"flpRelNrMain": null,
		"flpEmailMarkNY": 1,
		"relation": {
			"relNr": 309,
			"relRelNrMain": null,
			"relComNr": 9,
			"relType": null,
			"relName": "Slof",
			"relGender": 1,
			"relFirstName": "D",
			"relPrefix": "",
			"relRtlNr": 1,
			"relAddress1": "Stik van Linschoten 22",
			"relAddress2": "Stik van Linschoten 22",
			"relCity": "Bergdorp",
			"relPostalCode": "2411 PZ",
			"relState": null,
			"relCouNr": 1,
			"relPhone": "0172617000",
			"relPhoneMobile": "0172617000",
			"relPhoneWork": "0172617000",
			"relFax": null,
			"relEmail": "595@mms.nl",
			"relEmailVerifyCode": null,
			"relEmailWork": null,
			"relDateBirth": 717929,
			"relCreatedDate": null,
			"relHandicap": 25.1,
			"relGolferId": null,
			"relMemberCode": "142",
			"relMemberType": "a",
			"relExternalId": null,
			"relDebtorId": null,
			"relMaxCredit": null,
			"relValue1": null,
			"relValue2": null,
			"relValue3": null,
			"relGrfNr": null,
			"relGrfNrDiscount": null,
			"relRelNrCompany": null,
			"relDftContactYN": null,
			"relRelNrDebtor": null,
			"relRelNrMail": null,
			"relLngNr": null,
			"relEmpNr": null,
			"relRbrNr": null,
			"relEndBlockDate": null,
			"relGrpNr1": 136,
			"relGrpNr2": 2580,
			"relGrpNr3": 2581,
			"relDontCheckYN": null,
			"relPliNr": null,
			"relSupplierYN": null,
			"relImage": "https:\/\/s3-eu-west-1.amazonaws.com\/intogolf.nl\/new-avatar\/IoXwfFyPPg545W5LGDmhfy67ksQNCncor9e7e10C.jpeg",
			"relNotes": null,
			"relActualYN": 1,
			"relHandicapYN": null,
			"relCoursePermissionYN": null,
			"relBankAccount": null,
			"relChargeBankYN": null,
			"relRttNr": null,
			"relCallName": "Dirk",
			"relVatId": null,
			"relLastChanged": null,
			"relIBAN": null,
			"relBIC": null,
			"relGolfPermitDate": null,
			"relCoursePermDate": null,
			"relRulesExam": null,
			"relHomeclub": null,
			"full_name": "D Slof",
			"age": 55
		},
		"greenfee": {
			"grfNr": 1,
			"grfComNr": 9,
			"grfOrdering": 10,
			"grfName": "Standaard Greenfee",
			"grfDateFrom": 737790,
			"grfDateTo": 738155,
			"grfIntroYN": 0,
			"grfDiscountYN": 0,
			"grfCardYN": 0,
			"grfDiscountCardYN": 0,
			"grfRounds": 0,
			"grfRoundType": 1,
			"grfCardPrice": 0,
			"grfCardDuration": 12,
			"grfCardDurationType": 0,
			"grfActualYN": 0,
			"grfWildcardYN": 0,
			"grfFreePriceYN": 1,
			"grfCardDiscount": 0,
			"grfMlaNrCard": null,
			"grfMaxPerPeriod": 0,
			"grfMaxPeriod": 0
		},
		"sales": [],
		"rateList": {
			"1": {
				"grfNr": 7,
				"grfName": "Greenfeee",
				"grfCard": 0,
				"grfDiscountCard": 0,
				"grfDiscount": 0,
				"price": 0.02,
				"itmNr": 212,
				"gfcNr": null
			}
		},
		"discountList": [{
			"grfNr": 0,
			"grfName": "...",
			"grfCard": 0,
			"grfDiscountCard": 0,
			"grfDiscount": 1,
			"price": 0,
			"itmNr": 0
		}],
		"isSelected": false,
		"flpFullName": "D Slof (Dirk)"
	}]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

下面我们优化,只输出前台用到的字段

$result = Flight::query()
    ->select('fltNr', 'fltDate', 'fltTime1', 'fltTime2', 'fltCrlNr1', 'fltCrlNr2')
    ->with(['flightPlayers:flpNr,flpFltNr,flpBilNr,flpRelNr,flpGrfNr,flpScorecard',
        'flightPlayers.greenfee:grfNr,grfName,grfCardPrice,grfRoundType,grfOrdering,grfDateFrom,grfDateTo',
        'flightPlayers.relation:relNr,relName,relImage,relRtlNr,relMemberCode,relCity,relPhone,relGolferId,relAddress1,relDateBirth,relEmail,relHandicap,relFirstName,relCallName,relGender,relGrpNr1,relLastChanged',
        'flightPlayers.sales:salNr,salTrnNr,salBilNr,salItmNr,salVat,salBilNr,salAmount,salTrnNr',
        'flightPlayers.sales.item:itmNr,itmName,itmPrice,itmIsgNr,itmSellYN',
        'flightPlayers.sales.salesTransaction:trnNr,trnTimestamp'
    ])
    ->where(['fltNr' => $id])
    ->first()
1
2
3
4
5
6
7
8
9
10
11

需要注意,我们需要指定主键名称。 如 Flight 表和 flightPlayers 表是一对多关联,flpNr字段是flightPlayers表的主键,大概格式就是

->with[
'关联表1:关联表主键1,字段1,字段2'
'多级关联表1.多级关联表2:关联表主键2,字段1,字段2'
]
1
2
3
4
上次更新: 2022/9/23 13:38:29