首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改Laravel belongsToMany()返回的数据格式

更改Laravel belongsToMany()返回的数据格式
EN

Stack Overflow用户
提问于 2022-11-16 11:53:16
回答 1查看 31关注 0票数 0

我在我的Laravel模型中使用了belongsToMany(),我需要更改函数返回格式。

代码语言:javascript
复制
public function platform_information(): BelongsToMany
{
    return $this->belongsToMany(
        PlatformInformation::class,
        'platform_information_artist',
        'artist_id',
        'platform_information_id')->withPivot([
            'date', 'value'
        ]);
}

当我调用这个函数时

代码语言:javascript
复制
$artist->platform_information()
    ->orderBy('platform_information_id', 'asc')
    ->orderBy('date', 'asc')
    ->get()

返回以下数据:

代码语言:javascript
复制
[
        {
            "id": 1,
            "platform": "spotify",
            "information": "monthly_listeners",
            "description": null,
            "created_at": null,
            "updated_at": null,
            "deleted_at": null,
            "pivot": {
                "artist_id": 1,
                "platform_information_id": 1,
                "date": "2022-11-09",
                "value": 55400500
            }
        },
        {
            "id": 1,
            "platform": "spotify",
            "information": "monthly_listeners",
            "description": null,
            "created_at": null,
            "updated_at": null,
            "deleted_at": null,
            "pivot": {
                "artist_id": 1,
                "platform_information_id": 1,
                "date": "2022-11-10",
                "value": 55395190
            }
        },
        {
            "id": 2,
            "platform": "spotify",
            "information": "followers",
            "description": null,
            "created_at": null,
            "updated_at": null,
            "deleted_at": null,
            "pivot": {
                "artist_id": 1,
                "platform_information_id": 2,
                "date": "2022-11-09",
                "value": 25390584
            }
        },
        {
            "id": 2,
            "platform": "spotify",
            "information": "followers",
            "description": null,
            "created_at": null,
            "updated_at": null,
            "deleted_at": null,
            "pivot": {
                "artist_id": 1,
                "platform_information_id": 2,
                "date": "2022-11-10",
                "value": 25410584
            }
        }
    ]

所获得的数据是正确的,但不是我所需要的格式。这是我需要的格式:

代码语言:javascript
复制
[
  {
    "id": 1,
    "platform": "spotify",
    "information": "monthly_listeners",
    "data" : [
      {
        "artist_id": 1,
        "platform_information_id": 1,
        "date": "2022-11-09",
        "value": 55400500
      },
      {
        "artist_id": 1,
        "platform_information_id": 1,
        "date": "2022-11-10",
        "value": 55395190
      }
    ]
  },
  {
    "id": 2,
    "platform": "spotify",
    "information": "followers",
    "data" : [
      {
        "artist_id": 1,
        "platform_information_id": 2,
        "date": "2022-11-09",
        "value": 25390584
      },
      {
        "artist_id": 1,
        "platform_information_id": 2,
        "date": "2022-11-10",
        "value": 25410584
      }
    ]
  }
]

有任何方法可以直接使用belongsToMany()函数来完成这个任务吗?

还是我必须在控制器中手动完成?

EN

回答 1

Stack Overflow用户

发布于 2022-11-16 13:42:44

我还没有测试过它,但是我从这个问题中得到了什么,它应该是有效的。我假设您的模型目录中有一个模型艺术家。

代码语言:javascript
复制
$artist = \App\Models\Artist::with('platform_information')->get();

然后可以使用dd()来验证格式。

代码语言:javascript
复制
dd($artist->toArray());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74459994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档