我有三个表,其结构如下(它们在mysql数据库中)通过雄辩的模型连接到我的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 |

现在我做这样的查询:
$buildSets = array();

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

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

    $buildSets[] = $temp;
}

return $buildSets;

我的模特看起来是这样的:
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');
    }
}

我得到这样的结果(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": ""
      }
    ]
  }
]

如您所见,我在构建集中包含的“parts”数组中缺少标题、描述和颜色。
所以我的问题是,如何在我的回答中添加标题和颜色?我可以通过使用雄辩的模型来实现这一点吗,或者我必须在获取所有构建集的地方创建自己的db查询,然后迭代它们,获取所有部分和构建集部分,然后合并结果并将其添加到构建集。
任何人都有一个很好的解决方案,它将为我提供如下格式的零件数组中的项:
[
  {
    "title": "Part 1",
    "color": "Red",
    "amount": 25,
    "special_info": ""
  },
  {
    "title": "Part 2",
    "color": "Green",
    "amount": 75,
    "special_info": ""
  }
]

最佳答案

从你的模型中,你得到了BuildSetPart之间的多对多关系。
因此,在BuildSet中,可以使用hasManyThrough关系。
型号BuildSet

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

来源:http://laravel.com/docs/5.0/eloquent#relationships
无论如何,如果你想保持BuildSetBuildSetPart之间的关系。
我建议你改用这个。
public function buildSetPart()
{
   return $this->hasMany('App\Models\BuildSetPart');
}

注意:现在,您在parts()模型中同时得到buildSetPart()BuildSet
创建关系后,可以替换代码
$buildSets = array();

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

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

    $buildSets[] = $temp;
}

return $buildSets;

具有
return  BuildSet::with('parts')->get();

或者,如果您想要json格式的输出
return  BuildSet::with('parts')->toJson();

来源:http://laravel.com/docs/5.0/eloquent#collections

09-11 17:25