php - Change Laravel belongsToMany() returned data format

I'm using belongsToMany() in my Laravel model and i need to change the function return format.

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

When I call the function

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

Returns the following data:

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

The data obtained are correct, but not in the format in which I need them. This is the format that i need:

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

Is there any way to do this using the belongsToMany() function directly?

Or do I have to do it manually in the controller?

Answer

Solution:

I had not tested it but what I have get from the question, it should work. I am assuming that you have a model Artist in your Models directory.

$artist = \App\Models\Artist::with('platform_information')->get();

Then you can dd() to verify the format

dd($artist->toArray());

Source