Php Laravel 5雄辩的ORM多重关系
我在拉威尔是个全新的人,我正在尝试建立模型之间的关系。我的桌子是:Php Laravel 5雄辩的ORM多重关系,php,sql,laravel,orm,Php,Sql,Laravel,Orm,我在拉威尔是个全新的人,我正在尝试建立模型之间的关系。我的桌子是: patch id title area id location area_patch id patch_id area_id user_area_patch id area_patch_id plant_id plant_date 用户模型,“补丁”功能应执行如下操作: SELECT p.id, p.title, up.pla
patch
id
title
area
id
location
area_patch
id
patch_id
area_id
user_area_patch
id
area_patch_id
plant_id
plant_date
用户模型,“补丁”功能应执行如下操作:
SELECT
p.id, p.title, up.plant_id, up.plant_date
FROM
rsst_farming_patch p
JOIN rsst_farming_area_patch pp,
rsst_farming_user_patch up ON pp.id = up.area_patch_id AND p.id = pp.patch_id WHERE up.user_id = 1
我的模型:
class User extends Model {
public function patchs() {
//return user patchs
}
}
class Patch extends Model {
public function area() {
//return area that this patch belongs to
}
}
class Area extends Model {
public function patchs() {
//return available patchs
}
}
有人能举个例子吗?我想研究一下。我一直在摆弄用户模型,belongtomany和hasManyThrough,但运气不好。您可能需要稍微修改表结构以实现这一点 我在您的
user\u area\u patch
表中看到您试图将用户、区域和补丁链接在一起。这不是我在拉雷维尔的典型做法。通常情况下,您可以使用链接将两个项目链接在一起。因此,让我提出如下建议:
SELECT
p.id, p.title, up.plant_id, up.plant_date
FROM
rsst_farming_patch p
JOIN rsst_farming_area_patch pp,
rsst_farming_user_patch up ON pp.id = up.area_patch_id AND p.id = pp.patch_id WHERE up.user_id = 1
补丁是否属于单个用户?如果是这样,您应该向修补程序表中添加一个用户id
patch
id
user_id
area_id
title
一个补丁可以位于多个区域吗?我有点怀疑,所以我还要添加一个区域id
class User extends Model {
public function patchs() {
return $this->hasMany('App/Patch', 'user_id');
}
}
class Patch extends Model {
public function area() {
return $this->belongsTo('App\Area', 'area_id');
}
}
class Area extends Model {
public function patchs() {
return $this->hasMany('App\Patch', 'patch_id');
}
}
然后,您可以开始引用修补程序,如:
$patchs=User::find(1)->patchs()
或修补程序所属的区域
Patch::find(1)->area()
一个区域的所有补丁
Area::find(1)->patchs()
这有帮助吗?Laravel 5现在在关系声明中使用名称空间,您是否忘记了它们?还有,你能更明确地解释一下吗(?)你想做什么,怎么做!