如何在 Laravel 5 中加入模型?

Kun*_*Waz 5 php mysql laravel eloquent laravel-5

我有三个表,其结构如下(它们位于 MySQL 数据库中),通过 Eloquent 模型连接到我的 Laravel 5 API:

# build_sets table
| id | title | description |

# parts table
| id | title | description | color |

# build_set_parts table
| id | build_set_id | part_id | amount | special_info |
Run Code Online (Sandbox Code Playgroud)

目前我做了这样的查询:

$buildSets = array();

foreach(BuildSet::with('parts')->get() as $buildSet) {
    $temp = json_decode($buildSet);

    $temp->parts = $buildSet->parts;

    $buildSets[] = $temp;
}

return $buildSets;
Run Code Online (Sandbox Code Playgroud)

我的模型看起来像这样:

class BuildSet extends Model
{
    protected $table = 'build_sets';
    protected $hidden = ['created_at', 'updated_at'];

    public function parts()
    {
        return $this->hasMany('App\Models\BuildSetPart');
    }
}

class Part extends Model
{
    protected $table = 'parts';
    protected $hidden = ['id', 'created_at', 'updated_at'];

    public function buildSets()
    {
        return $this->hasMany('App\Models\BuildSet');
    }
}

class BuildSetPart extends Model
{
    protected $table = 'build_set_parts';
    protected $hidden = ['id', 'build_set_id', 'part_id', 'created_at', 'updated_at'];

    public function buildSet()
    {
        return $this->belongsTo('App\Models\BuildSet');
    }

    public function part()
    {
        return $this->belongsTo('App\Models\Part');
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到这样的结果(JSON 响应):

[
  {
    "id": 1,
    "title": "Build set 1",
    "description": "This is a small build set.",
    "parts": [
      {
        "amount": 150,
        "special_info": ""
      },
      {
        "amount": 400,
        "special_info": "Extra strong"
      },
      {
        "amount": 25,
        "special_info": ""
      }
    ]
  },
  {
    "id": 2,
    "title": "Build set 2",
    "description": "This is a medium build set.",
    "parts": [
      {
        "amount": 150,
        "special_info": ""
      },
      {
        "amount": 400,
        "special_info": "Extra strong"
      },
      {
        "amount": 25,
        "special_info": ""
      },
      {
        "amount": 25,
        "special_info": ""
      },
      {
        "amount": 25,
        "special_info": ""
      }
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我缺少构建集中包含的“零件”数组中的标题、描述和颜色。所以我的问题是,如何将标题和颜色添加到我的回复中?我可以通过使用 Eloquent 模型来做到这一点,还是必须创建自己的数据库查询,在其中获取所有构建集,然后迭代它们并获取所有部分和构建集部分,然后合并该结果并将其添加到构建集。

任何人都可以找到一个好的解决方案,它将为我提供格式如下的零件数组中的项目:

[
  {
    "title": "Part 1",
    "color": "Red",
    "amount": 25,
    "special_info": ""
  },
  {
    "title": "Part 2",
    "color": "Green",
    "amount": 75,
    "special_info": ""
  }
]
Run Code Online (Sandbox Code Playgroud)

Kun*_*Waz 3

我找到了一个几乎与我想要的解决方案一样的解决方案,我尝试了 mininoz 解决方案,发现我在数据透视表关系方面遇到了一些问题。

我现在做这个查询:

return BuildSet::with('parts')->get();
Run Code Online (Sandbox Code Playgroud)

我的模型现在看起来像这样,请注意我在末尾添加的“->withPivot()”函数,它允许我“访问”数据透视表中的列:

class BuildSet extends Model
{
    protected $table = 'build_sets';
    protected $hidden = ['created_at', 'updated_at'];

    public function parts()
    {
        return $this->belongsToMany('App\Models\BuildSetPart', 'build_set_parts')->withPivot('amount', 'special_info');
    }
}

class Part extends Model
{
    protected $table = 'parts';
    protected $hidden = ['id', 'created_at', 'updated_at'];

    public function buildSets()
    {
        return $this->belongsToMany('App\Models\BuildSet', 'build_set_parts')->withPivot('amount', 'special_info');
    }
}

class BuildSetPart extends Model
{
    protected $table = 'build_set_parts';
    protected $hidden = ['id', 'build_set_id', 'part_id', 'created_at', 'updated_at'];
}
Run Code Online (Sandbox Code Playgroud)

现在,我在构建集响应中获得了扩展部件响应(注意,我仅在此处显示部件数据,构建集数据与之前的“部件”数组一样):

[
  {
    "title": "Part 1",
    "color": "Red",
    "pivot": {
      "build_set_id": 1,
      "part_id": 1,
      "amount": 25,
      "special_info": ""
    }
  },
  {
    "title": "Part 2",
    "color": "Green",
    "pivot": {
      "build_set_id": 1,
      "part_id": 2,
      "amount": 55,
      "special_info": ""
    }
  }
]
Run Code Online (Sandbox Code Playgroud)

此响应包含我需要的所有内容,但如果我想将其全部放在 JSON 响应层次结构中的同一“级别”上,则需要进行转换。