预加载关系表 eloquent
eager loading eloquent for relationship tables
如何加载 eloquent 低于 table 的蛋类加载。
我想得到paul->donation[{eldery,material->clothes}, {orphan,material->aircon}]
我如何通过 laravel eloquent 预加载获得它?
下面是数据库结构和内容
Members
+----+-------------------+
| id | name |
+----+-------------------+
| 1 | Paul |
| 2 | John |
| 3 | kathy |
| 4 | Mary |
+----+-------------------+
donation
+----+-------------------+
| id | name |
+----+-------------------+
| 1 | eldery care |
| 2 | orphan school |
+----+-------------------+
donation_member pivot
+----+-----------+-----------+
| id | member_id | donate_id |
+----+-----------+-----------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 4 | 1 |
| 4 | 1 | 2 |
| 5 | 3 | 2 |
| 6 | 4 | 2 |
+----+-----------+-----------+
Required Material
+----+---------------+----------------+--------------+--------------+
| id | name | donation_id | priority | is_donated |
+----+---------------+----------------+--------------+--------------+
| 1 | medicine | 1 | 1 | 1 |
| 2 | clothes | 1 | 2 | 0 |
| 3 | aircondition | 2 | 1 | 0 |
| 4 | Desktop | 2 | 2 | 0 |
+----+--------------------------------+--------------+--------------+
更容易理解的是使用惰性渴望加载。我也喜欢使用它。首先获取对象或对象的集合,然后加载关系。
$members = Member::where('material.is_donated', false)->where('priority', youngest)->get();
$members->load("donation");
$members->load("material");
为此,您需要在 class 成员中定义 Eloquent 关系方法 donation() 和 material()。
加载嵌套关系和平面关系一样简单,您还可以添加约束:
// Not sure what you're trying to do with the priority, I guess
// you just want the highest one?
$highestPriority = 3;
$members = Members::with([
'donations',
'donations.material' => function($query) use ($highestPriority) {
$query->where('is_dontated', false);
$query->where('priority', $highestPriority);
}
])->get();
但是:这些约束只适用于关系子集,而不适用于整个集合。这意味着,如果您只想获得具有最高优先级 material 的所有成员和捐赠,您要么必须
- 使用获取的集合(类型为 Illuminate\Support\Collection),可能类似于
$members->filter(...)
、
- 使用联接,或
- 从反比关系(
Material::where(['is_donated' => true, 'priority' => $highest)])->with(['donations', 'donations.member'])->get()
;
如何加载 eloquent 低于 table 的蛋类加载。
我想得到paul->donation[{eldery,material->clothes}, {orphan,material->aircon}]
我如何通过 laravel eloquent 预加载获得它?
下面是数据库结构和内容
Members
+----+-------------------+
| id | name |
+----+-------------------+
| 1 | Paul |
| 2 | John |
| 3 | kathy |
| 4 | Mary |
+----+-------------------+
donation
+----+-------------------+
| id | name |
+----+-------------------+
| 1 | eldery care |
| 2 | orphan school |
+----+-------------------+
donation_member pivot
+----+-----------+-----------+
| id | member_id | donate_id |
+----+-----------+-----------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 4 | 1 |
| 4 | 1 | 2 |
| 5 | 3 | 2 |
| 6 | 4 | 2 |
+----+-----------+-----------+
Required Material
+----+---------------+----------------+--------------+--------------+
| id | name | donation_id | priority | is_donated |
+----+---------------+----------------+--------------+--------------+
| 1 | medicine | 1 | 1 | 1 |
| 2 | clothes | 1 | 2 | 0 |
| 3 | aircondition | 2 | 1 | 0 |
| 4 | Desktop | 2 | 2 | 0 |
+----+--------------------------------+--------------+--------------+
更容易理解的是使用惰性渴望加载。我也喜欢使用它。首先获取对象或对象的集合,然后加载关系。
$members = Member::where('material.is_donated', false)->where('priority', youngest)->get();
$members->load("donation");
$members->load("material");
为此,您需要在 class 成员中定义 Eloquent 关系方法 donation() 和 material()。
加载嵌套关系和平面关系一样简单,您还可以添加约束:
// Not sure what you're trying to do with the priority, I guess
// you just want the highest one?
$highestPriority = 3;
$members = Members::with([
'donations',
'donations.material' => function($query) use ($highestPriority) {
$query->where('is_dontated', false);
$query->where('priority', $highestPriority);
}
])->get();
但是:这些约束只适用于关系子集,而不适用于整个集合。这意味着,如果您只想获得具有最高优先级 material 的所有成员和捐赠,您要么必须
- 使用获取的集合(类型为 Illuminate\Support\Collection),可能类似于
$members->filter(...)
、 - 使用联接,或
- 从反比关系(
Material::where(['is_donated' => true, 'priority' => $highest)])->with(['donations', 'donations.member'])->get()
;