让我们考虑我有一个模型User
和另一个模型Employee
现在,我只想选择Employee模型的某些字段,例如 salary,id,emp_id ,以及User模型的某些列,例如 name,id 。
>$employee = Employee::with('user:id,name')
->where('department', $request->department)
->get(['id', 'emp_id', 'salary']);
当我执行此操作时,它将返回 id,emp_id,工资数据,但对于 user:name,id ,它将返回 null >
如何在get()中指定 user:id,name?
答案 0 :(得分:1)
您可以加载完整模型或仅加载其中的某些字段。要仅加载某些字段,请使用<form name="form1" method="post" action="./mutualmarket.aspx?t=hk%2f%3f" id="form1" style="text-align: left;">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="bKL6Py6JoD+UqJiQ0rYby89kuE5F9cbnJNFYPp2spoXRpiUHWXMPRG8zwk8PILbZSbn81reaeg6k3H5YU/r2NPcdO0WwyXcyC7YqTiDi3xpXEPWm654UtThRL5HgsHmMZGRMMxBPiHUTA+GtplZ6t/s8chsO/dfnCHXzOQWQ6CazRo80IBYnDTrKY/6q9hx/YDRu+g==">
</div>
<div>
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="3C67932C">
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="HI5FxZFHKh1Q5D5ExYCAkTbgozWeAb/DOPLTwntzLbpFZaOqvYTa/jniEDrvg53syrn/3EpGt7IYjns+KXq6FaUppQRtjT2EosDG5wmuGEaf+WuKFRqUtRpcS2MGWJMEqbwTfiRyy7CLqcD1TqRBn/oey+UZyHCE/P2GpkQiYpVIZEABJdeW3ehj57EP+yDZcuWgAUtnjHjnkNk54+jTlD5r7S+lT8FGYm1uoytV7detJ5KL9XUI/By3iKETPSunsOzb8+zBMOYheIk1/7QiDDoAHOUcmelTLz6YmCM4j+xZD00nvVjMnLCERoUV/A32fidSnzaZEBk+Mgi8nVTZ+71u95p/D/ELwN/xeUen3cEy/cgYlyHi3MjLof3SSxOOWQUflufPA+kAnTT0XgoOguN/UO3QBfcvWAqgpWTPSujx6FLdSu7r9bk7DyBDD+uwqB5jKQkk+zqEoR1ODZ+Zp8VNqezsmPNczSBihHri207Hd++oHVKGlpbRG2KUBK0ngNjSQ2Gqqeuq4lOOCEl41JzBlHYZBIUSHp5tI2L61aR2gGFjVkPxyE8doM2EZZgdIUl9lDiWmTvtNU7nS12QC9v4e4whz1NSqC1LUOA/JVWiJ0KYdXG4AGyArcosOGZQF6eaFzkWw9ooak2LXPYbUb1pt+S+NfXk3vDpalfC+bthIukOas/QC8+RXQF5oiSZyNo3Znj+/jj1IvDwl7kJHR76k2XFeIb5Z+mTzBJMphze5S/VzkHN3E1vaFKQErQj5Y62wYssyfUKamCj21FhH6Era6IEiBxnUIinCIY/6387sBNlrdUabBfxK0Vm3gEjrGU/i5hSTmcn+t5/XBC6T8Xx5cVGIZUKDMtmFqkFF4RZtRpsuKfT1VyW0gDK26YrYW14bx19Tgm+8u2HmvvM7ZzbC1pabCJnlN6RhYswtm8aT7aq9TqcmV9wPfewHr5LOZaAsCjE+qTF+vrU1v2tmI4p7IevlhnomZO4BRD8bAogJF6LCWNUmxdTu5ewP4MJrOiP7cMDmuLHoiExdWL1PQOFh/E16JXw6liJuhk38+dG5eKzwt3bdeX3iT44XzHReE6pUK4G4nQIipa5LQFvXpsrCQ4lsn+BiXUdHtjhSG6S3kyYipOQrLEcSjYsHxZdUZDz4KmrhcEEcTVKXT4xxCFiQ74=">
</div>
<input type="hidden" name="today" id="today" value="20180623">
<input type="hidden" name="sortBy" id="sortBy">
<input type="hidden" name="alertMsg" id="alertMsg">
。它也可以在关系热切的加载查询中工作:
select()
答案 1 :(得分:0)
使用Query bulider中的select()
方法:
$employee = Employee::with('user') // or users <---
->select(['id', 'emp_id', 'salary', 'users.id AS user_id', 'users.name AS username'])
->where('department', $request->department)
->get();