简体   繁体   English

Laravel:JSON 和数据透视表

[英]Laravel: JSON and pivot table

Sorry about the non-explanatory title but I could not come up with a descriptive one.对非解释性的标题感到抱歉,但我无法想出一个描述性的标题。

I've got the following 3 tables: - games - platforms - games_platforms我有以下 3 个表: - 游戏 - 平台 - games_platforms

And I've got 2 Models in Laravel for both Platform and Game.我在 Laravel 中为平台和游戏提供了 2 个模型。

public function games() 
{
    return $this->belongsToMany('App\Game', 'games_platforms')->withPivot('release_date');
}

public function platforms() 
{
    return $this->belongsToMany('App\Platform', 'games_platforms')->withPivot('release_date');
}

Now this works like a charm, I get a JSON string with all the information in the 3 tables, like this.现在这就像一个魅力,我得到了一个包含 3 个表中所有信息的 JSON 字符串,就像这样。

[{
    "id": 1,
    "name": "Borderlands",
    "short_description": "",
    "created_at": null,
    "updated_at": null,
    "platforms": [{
        "id": 4,
        "name": "PC",
        "pivot": {
            "game_id": 1,
            "platform_id": 4,
            "release_date": "2016-03-03"
        }
    }]
}]

Now my problem is as follows.现在我的问题如下。 I don't want to show the whole 'pivot' information, just the 'release_date', like this:我不想显示整个“枢轴”信息,只显示“发布日期”,如下所示:

"platforms": [{
        "id": 4,
        "name": "PC",
        "release_date": "2016-03-03"

Is there an easy way in Laravel to do such a thing? Laravel 有没有简单的方法来做这样的事情? As far as I can see right now, looking at other posts, is to either write a function that turns the json into an array and I can arrange it then.就我现在所见,查看其他帖子,要么编写一个将 json 转换为数组的函数,然后我就可以安排它。 Or I can write my own query instead of letting Laravel do all that.或者我可以编写自己的查询,而不是让 Laravel 做所有这些。

Hope you guys can help me with this issue.希望你们能帮我解决这个问题。 Thanks!谢谢!

I would modify the data returned from the query via methods on the collection class:我将通过集合类上的方法修改从查询返回的数据:

//replace Game::all() with your actual query
return Game::all()->each(function($game){
    $game->platforms->map(function($platform){
        $platform->release_date = $platform->pivot->release_date;
        unset($platform->pivot);
        return $platform;
    });
});

I know this is already answered but I believe the proper answer would be to add whatever you want to hide to your hidden attribute on the model.我知道这已经得到了回答,但我相信正确的答案是将您想要隐藏的任何内容添加到模型的隐藏属性中。

<?php
class Games extends Eloquent
{
    protected $hidden = ['pivot.game_id', 'pivot.platform_id'];
}

I am not sure what your keys are becuase they are different in your two examples.我不确定你的钥匙是什么,因为它们在你的两个例子中是不同的。 Please see: https://github.com/laravel/framework/issues/745请参阅: https : //github.com/laravel/framework/issues/745

A better way is to use Laravel resources,更好的方法是使用 Laravel 资源,

First create Resource ( php artisan make:resource )首先创建资源( php artisan make:resource

Rresource GameResource extends Resource
{
 public function toArray($request)
 {
  return [
    'release_date' => $this->pivot->release_date,
    'name' => $this->name,
     /// All other fields or you can merge both here 
   ];
 }
}

Now use this resource;现在使用这个资源;

$data = GameResource::collection(Game::all());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM