我有3张 table
Users Table
+----+-------+----------+
| id | email | password |
+----+-------+----------+
| 1 | 1 | John |
+----+-------+----------+
user_details Table
+----+--------------------+------------+-----------+
| id | user_id [FK_users] | first_name | last_name |
+----+--------------------+------------+-----------+
| 1 | 1 | John | Kevin |
+----+--------------------+------------+-----------+
Posts Table
+----+--------------------+----------+
| id | user_id [FK_users] | title |
+----+--------------------+----------+
| 1 | 1 | 1st Post |
+----+--------------------+----------+
现在我已经在模型中创建了所有关系,现在我可以通过下面的eloquent查询访问user_details表,它返回整个用户表和user_details表,但我只想从user_details表中选择first_name和last_name我该怎么做?
$posts= Post::with('city:id,name_en', 'user.userDetail')->where('id', $id)->get();
1 回答
这将获取所有'post'属性,'user'
id
,'user_details'user_id, first_name, last_name