Php 如何加入Laravel 5中的模型?
我有三个表,结构如下(它们位于MySQL数据库中),通过雄辩的模型连接到我的Laravel 5 API:Php 如何加入Laravel 5中的模型?,php,mysql,laravel,eloquent,laravel-5,Php,Mysql,Laravel,Eloquent,Laravel 5,我有三个表,结构如下(它们位于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 | 目前,我进行如下查询: $bu
# 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');
}
}
[
{
"title": "Part 1",
"color": "Red",
"amount": 25,
"special_info": ""
},
{
"title": "Part 2",
"color": "Green",
"amount": 75,
"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');
}
}
[
{
"title": "Part 1",
"color": "Red",
"amount": 25,
"special_info": ""
},
{
"title": "Part 2",
"color": "Green",
"amount": 75,
"special_info": ""
}
]
我得到的结果如下(JSON响应):
正如您所看到的,我在构建集中包含的“parts”数组中缺少标题、描述和颜色。
所以我的问题是,如何在回复中添加标题和颜色?我可以通过使用雄辩的模型来做到这一点,还是我必须进行自己的DB查询,从中获取所有构建集,然后迭代它们,获取所有部分和构建集部分,然后合并该结果并将其添加到构建集
任何人都有一个很好的解决方案,可以在部件数组中为我提供如下格式的项目:
$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');
}
}
[
{
"title": "Part 1",
"color": "Red",
"amount": 25,
"special_info": ""
},
{
"title": "Part 2",
"color": "Green",
"amount": 75,
"special_info": ""
}
]
从您的模型中,您得到了
BuildSet
和Part
之间的多对多关系
因此,在BuildSet
中,可以使用hasManyThrough
关系
模型BuildSet
public function parts()
{
return $this->hasManyThrough('App\Models\Part', 'App\Models\BuildSetPart');
}
资料来源:
无论如何,如果您想保持BuildSet
和BuildSetPart
之间的关系
我建议你用这个代替
public function buildSetPart()
{
return $this->hasMany('App\Models\BuildSetPart');
}
注意:现在,您在BuildSet
模型中获得了parts()
和buildSetPart()
创建关系后,可以替换代码
$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();
来源:我找到了一个几乎符合我想要的解决方案,我尝试了mininoz解决方案,发现pivot表关系存在一些问题 我现在进行以下查询:
return BuildSet::with('parts')->get();
我的模型现在看起来是这样的,注意我在最后添加的“->withPivot()”函数,它允许我“访问”pivot表中的列:
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'];
}
现在,我在构建集响应中得到了一个扩展的部件响应(注意,我在这里只显示部件数据,构建集数据与前面的“部件”数组相同):
此响应包含我所需的所有内容,但如果我想在JSON响应层次结构中的同一“级别”上获得所有内容,我需要进行转换。感觉好像存在误解,我似乎无法通过透视表(build\u set\u parts)进行连接,并从该表以及表(parts)中获取额外信息链接到透视表。我使用了一些idéas,但必须使用“->withPivot()”函数才能使其正常工作,请参阅我的答案以获取信息。