【问题标题】:Change Laravel belongsToMany() returned data format更改 Laravel belongsToMany() 返回的数据格式
【发布时间】:2022-11-16 21:52:48
【问题描述】:

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

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

当我调用函数时

$artist->platform_information()
    ->orderBy('platform_information_id', 'asc')
    ->orderBy('date', 'asc')
    ->get()

返回以下数据:

[
        {
            "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
            }
        }
    ]

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

[
  {
    "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()函数来做到这一点?

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

【问题讨论】:

  • 我建议在控制器中操纵响应并让关系做它​​现在所做的事情,因为如果您更改“标准”关系的返回,它可能会变得非常不方便
  • 有没有可能直接从函数中执行它,因为这个查询会产生很多结果,而且我认为如果可以直接从函数中完成,那么在控制器中手动执行它并不是最佳选择。
  • 我不会在该函数中执行此操作,但如果您不想在控制器中执行此操作,则会在模型中的其他函数中执行。恐怕它会产生大量数据这一事实是一种不同的讨论。

标签: php laravel eloquent laravel-8


【解决方案1】:

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

$artist = AppModelsArtist::with('platform_information')->get();

然后你可以 dd() 来验证格式

dd($artist->toArray());

【讨论】:

    猜你喜欢
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 2023-01-02
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多