Php 如何使用Eloquent::with方法获取特定于数据库的列的嵌套关系?
我有以下三个表格: 发布用户信息Php 如何使用Eloquent::with方法获取特定于数据库的列的嵌套关系?,php,mysql,laravel,eloquent,lumen,Php,Mysql,Laravel,Eloquent,Lumen,我有以下三个表格: 发布用户信息 ------------------------------------------------- Post ------------------------------------------------- id | user_id | content | created_at | updated_at 建立的关系如下: User.php ... public function userInfo() {
-------------------------------------------------
Post
-------------------------------------------------
id | user_id | content | created_at | updated_at
建立的关系如下:
User.php
...
public function userInfo() {
return $this->hasOne('App\UserInfo');
}
...
public function posts() {
return $this->hasMany('App\Post');
}
...
...
public function user() {
return $this->belongsTo('App\User');
}
...
...
public function user() {
return $this->belongsTo('App\User');
}
...
UserInfo.php
...
public function userInfo() {
return $this->hasOne('App\UserInfo');
}
...
public function posts() {
return $this->hasMany('App\Post');
}
...
...
public function user() {
return $this->belongsTo('App\User');
}
...
...
public function user() {
return $this->belongsTo('App\User');
}
...
Post.php
...
public function userInfo() {
return $this->hasOne('App\UserInfo');
}
...
public function posts() {
return $this->hasMany('App\Post');
}
...
...
public function user() {
return $this->belongsTo('App\User');
}
...
...
public function user() {
return $this->belongsTo('App\User');
}
...
我得到的所有帖子都有用户和用户信息嵌套数据,如下所示:
Post::with('user.userInfo')
{
"data": [
{
"id": 1,
"created_at": "2020-08-01 06:10:00",
"updated_at": "2020-08-01 06:10:00",
"user_id": 1,
"content": "My first post!",
"user": {
"id": 1,
"user_info_id": 1,
"email": "myemail@gmail.com",
"username": "derek",
"access_token": "secret",
"created_at": "2020-08-01 04:15:09",
"updated_at": "2020-08-01 04:15:09",
"user_info": {
"id": 1,
"user_id": 1,
"name": "Derek Baker",
"web": "https://github.com/derek90",
"birthday": "1990-11-27",
"gender": "M",
"bio_description": "Software Developer",
"profile_picture": null
}
}
},
{
"id": 2,
"created_at": "2020-08-01 06:09:54",
"updated_at": "2020-08-01 06:09:54",
"user_id": 1,
"content": "My second post!",
"user": {
"id": 1,
"user_info_id": 1,
"email": "myemail@gmail.com",
"username": "derek",
"remember_token": null,
"access_token": "secret",
"created_at": "2020-08-01 04:15:09",
"updated_at": "2020-08-01 04:15:09",
"user_info": {
"id": 1,
"user_id": 1,
"name": "Derek Baker",
"web": "https://github.com/derek90",
"birthday": "1990-11-27",
"gender": "M",
"bio_description": "Software Developer",
"profile_picture": null
}
}
}
]
}
返回如下内容:
Post::with('user.userInfo')
{
"data": [
{
"id": 1,
"created_at": "2020-08-01 06:10:00",
"updated_at": "2020-08-01 06:10:00",
"user_id": 1,
"content": "My first post!",
"user": {
"id": 1,
"user_info_id": 1,
"email": "myemail@gmail.com",
"username": "derek",
"access_token": "secret",
"created_at": "2020-08-01 04:15:09",
"updated_at": "2020-08-01 04:15:09",
"user_info": {
"id": 1,
"user_id": 1,
"name": "Derek Baker",
"web": "https://github.com/derek90",
"birthday": "1990-11-27",
"gender": "M",
"bio_description": "Software Developer",
"profile_picture": null
}
}
},
{
"id": 2,
"created_at": "2020-08-01 06:09:54",
"updated_at": "2020-08-01 06:09:54",
"user_id": 1,
"content": "My second post!",
"user": {
"id": 1,
"user_info_id": 1,
"email": "myemail@gmail.com",
"username": "derek",
"remember_token": null,
"access_token": "secret",
"created_at": "2020-08-01 04:15:09",
"updated_at": "2020-08-01 04:15:09",
"user_info": {
"id": 1,
"user_id": 1,
"name": "Derek Baker",
"web": "https://github.com/derek90",
"birthday": "1990-11-27",
"gender": "M",
"bio_description": "Software Developer",
"profile_picture": null
}
}
}
]
}
我想要的是只获取每个实体的几列,如下所示:
{
"data": [
{
"id": 1,
"created_at": "2020-08-01 06:10:00",
"updated_at": "2020-08-01 06:10:00",
"content": "My first post!",
"user": {
"id": 1,
"username": "derek",
"user_info": {
"name": "Derek Baker",
"profile_picture": null
}
}
},
{
"id": 2,
"created_at": "2020-08-01 06:09:54",
"updated_at": "2020-08-01 06:09:54",
"content": "My second post!",
"user": {
"id": 1,
"username": "derek",
"user_info": {
"name": "Derek Baker",
"profile_picture": null
}
}
}
]
}
有没有一种方法可以通过使用Post::with
elount函数来实现这一点
我已经尝试了Post::with('user:id,username','user.userInfo')
,它对用户列很有效,但是userInfo提供了它的所有功能
我尝试过的其他事情:
Post::with('user:id,username','user.userInfo:name,profile\u picture')
将在json字段中显示“user\u info”:null
Post::with('user:id,username','user.userInfo:userInfo.name,userInfo.profile\u picture')
显示错误未知列“field list”中的“userInfo.name”
使用user.userInfo:user.userInfo.name,user.userInfo.profile\u picture
和user.userInfo:user.user\u info.name,user.user\u info.profile\u picture
可以使用API资源
API资源充当一个转换层,位于
雄辩的模型和实际返回给用户的JSON响应
应用程序的用户
您可以为帖子创建API资源,并在响应中返回帖子的任何位置使用它
Api资源为您提供了更多的控制,您可以操作任何需要的字段,使用几个字段的组合发送一些额外字段,更改响应中需要的字段的名称(xyz=>$this->name
)
PostResource
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class PostResource extends JsonResource
{
public function toArray($request)
{
//You can access model properties directly using $this
return [
"id" => $this->id,
"created_at" => $this->created_at,
"updated_at" => $this->updated_at,
"content" => $this->content,
"user" => [
"id" => $this->user->id,
"username" => $this->user->username,
"user_info" => [
"name" => $this->user->userInfo->name,
"profile_picture" => $this->user->userInfo->profile_picture,
]
]
];
}
}
如果你有收藏
// $posts is a collection of Post Model instances.
return PostResource::collection($posts);
PostResource将应用于集合中的每个模型实例,然后作为JSON响应返回给您
[注意]
您可以为用户和任何模型创建类似的资源。它使您更容易根据自己的喜好定制响应
同样,在上面的例子中。您可以简单地将user\u info属性仅添加到用户,而不是将user\u info包含在user中
return [
"id" => $this->id,
"created_at" => $this->created_at,
"updated_at" => $this->updated_at,
"content" => $this->content,
"user" => [
"id" => $this->user->id,
"username" => $this->user->username,
"name" => $this->user->userInfo->name,
"profile_picture" => $this->user->userInfo->profile_picture,
]
];
有趣。谢谢我正在尝试应用这个解决方案,但我必须找出如何使它与排序结果一起工作。(我拥有的是
Post::with('user:id,username','user.userInfo')->orderBy('created_at','DESC')->paginate(10,['page'=>$pageNumber])
)哦,别担心,你可以直接将分页实例传递给集合方法,就像这样PostResource::collection('paginated_instance)
您可以始终将paginator实例传递给资源的collection方法
是的,我这样做了,但它只返回posts列表。我丢失了->paginate(10)在json数据字段(当前页面等)中包装帖子时提供给我的所有其他文件。我需要这个信息发送到前端,以及知道哪一页的要求。我应该在不同的API资源中添加这些字段吗?不,您不应该单独添加,因为它应该提供文档中提到的meta和link字段。这就是我正在做的:$posts=Post::with('user:id,username','user.userInfo')->orderBy('created_at','DESC')->paginate(10,['page'=>$pageNumber])代码>returnresponse()->json(PostResource::collection($posts),200)