如何在 Laravel 5 中加入模型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29562274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to join models in Laravel 5?
提问by KungWaz
I have three tables, with a structure like this (they are in an MySQL DB) connected to my Laravel 5 API with Eloquent models:
我有三个表,具有这样的结构(它们在 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 |
At the moment I do a query like this:
目前我做这样的查询:
$buildSets = array();
foreach(BuildSet::with('parts')->get() as $buildSet) {
$temp = json_decode($buildSet);
$temp->parts = $buildSet->parts;
$buildSets[] = $temp;
}
return $buildSets;
And my models look like this:
我的模型看起来像这样:
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');
}
}
I get a result like this (JSON response):
我得到这样的结果(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": ""
}
]
}
]
As you can see I'm missing the title, description and color in the "parts" array which is included in the build set. So my question is, how to I add the title and the color to my response? Can I do that by using Eloquent models, or do I have to make my own DB query where I get all build sets and then iterate them and get all the parts and build set parts and then merge that result and add it to the build set.
如您所见,我在构建集中包含的“零件”数组中缺少标题、描述和颜色。所以我的问题是,如何在回复中添加标题和颜色?我可以通过使用 Eloquent 模型来做到这一点,还是必须创建自己的数据库查询,在其中获取所有构建集,然后迭代它们并获取所有部件和构建集部件,然后合并该结果并将其添加到构建集.
Any one got a good solution which will give me items in the parts array formatted like this:
任何人都得到了一个很好的解决方案,它将为我提供如下格式的部分数组中的项目:
[
{
"title": "Part 1",
"color": "Red",
"amount": 25,
"special_info": ""
},
{
"title": "Part 2",
"color": "Green",
"amount": 75,
"special_info": ""
}
]
采纳答案by KungWaz
I found a solution that is almost as I wanted to do it, I tried mininoz solution and found out that I get some problems with the pivot table relation.
我找到了一个几乎和我想做的一样的解决方案,我尝试了 mininoz 解决方案,发现我在数据透视表关系方面遇到了一些问题。
I now do this query:
我现在做这个查询:
return BuildSet::with('parts')->get();
And my models now looks like this, note the "->withPivot()" function I added at the end, it allows me to "access" the columns in my pivot table:
我的模型现在看起来像这样,请注意我在最后添加的“->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'];
}
I now get a extended parts response in my build set response (Note, I only show the parts data here, the build set data is as before with an "parts" array):
我现在在我的构建集响应中得到一个扩展的部件响应(注意,我在这里只显示部件数据,构建集数据和以前一样,带有一个“部件”数组):
[
{
"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": ""
}
}
]
This response contains all I need, but I need to transform if I want to get it all on the same "level" in the JSON response hierarchy.
此响应包含我需要的所有内容,但如果我想将其全部置于 JSON 响应层次结构中的同一“级别”,则需要进行转换。
回答by mininoz
From you model, You got Many to Many relationship between BuildSet
and Part
.
从你的模型中,你有BuildSet
和之间的多对多关系Part
。
Therefore, In BuildSet
, you can use hasManyThrough
relationship.
因此,在 中BuildSet
,您可以使用hasManyThrough
关系。
Model BuildSet
模型 BuildSet
public function parts()
{
return $this->hasManyThrough('App\Models\Part', 'App\Models\BuildSetPart');
}
source: http://laravel.com/docs/5.0/eloquent#relationships
来源:http: //laravel.com/docs/5.0/eloquent#relationships
Anyway, if you want to keep the relationship between BuildSet
and BuildSetPart
.
无论如何,如果你想保持的关系BuildSet
和BuildSetPart
。
I suggest you to use this instead.
我建议你改用这个。
public function buildSetPart()
{
return $this->hasMany('App\Models\BuildSetPart');
}
note: right now, you got both parts()
and buildSetPart()
in BuildSet
model
注:现在,你有两个parts()
和buildSetPart()
在BuildSet
模型
After you create the relationship, you can replace you code
创建关系后,您可以替换代码
$buildSets = array();
foreach(BuildSet::with('parts')->get() as $buildSet) {
$temp = json_decode($buildSet);
$temp->parts = $buildSet->parts;
$buildSets[] = $temp;
}
return $buildSets;
with
和
return BuildSet::with('parts')->get();
or, if you want the output in JSON format
或者,如果您想要 JSON 格式的输出
return BuildSet::with('parts')->toJson();