使用嵌套的多对多项目查询Laravel的多态关系

huangapple go评论92阅读模式
英文:

Laravel querying polymorphic relationships with nested many to many items

问题

以下是您要翻译的内容:

  1. 我有4个表格,它们之间有以下关系:
  1. class Note extends Model
  2. {
  3. public function noteable() {
  4. return $this->morphTo();
  5. }
  6. }
  7. class Expense extends Model
  8. {
  9. public function notes()
  10. {
  11. return $this->morphMany(Note::class, 'noteable');
  12. }
  13. }
  14. class Review extends Model
  15. {
  16. public function notes()
  17. {
  18. return $this->morphMany(Note::class, 'noteable');
  19. }
  20. public function repairs()
  21. {
  22. return $this->belongsToMany(Repair::class);
  23. }
  24. }
  25. class Repair extends Model
  26. {
  27. public function reviews()
  28. {
  29. return $this->belongsToMany(Review::class);
  30. }
  31. }
  1. 正如您所看到的,Note 是一对多多态关系,而 Reviews Repairs 是多对多关系。
  2. 我想要获取所有与 Reviews 关联并且同时具有 Repairs Notes。我该如何执行这样的操作?
  3. 根据文档,我尝试像这样做:
  1. $notes = App\Note::query()
  2. ->with(['noteable' => function (MorphTo $morphTo) {
  3. $morphTo->morphWith([
  4. Review::class => ['repairs']
  5. ]);
  6. }])->get();
  1. 我希望我的查询应该返回类似以下的结果:
  1. [
  2. {
  3. "id": 11,
  4. "noteable_id": 4,
  5. "noteable_type": "App\\Expense",
  6. "noteable": {
  7. "id": 4,
  8. "name": "Expense",
  9. "category": "general"
  10. }
  11. },
  12. {
  13. "id": 13,
  14. "noteable_id": 5,
  15. "noteable_type": "App\\Review",
  16. "noteable": {
  17. "id": 5,
  18. "name": "Review 5",
  19. "mileage": 120000,
  20. "repairs": [..., ...] // 我所需的内容
  21. }
  22. }
  23. ]
英文:

I have 4 tables with the following relationships:

  1. class Note extends Model
  2. {
  3. public function noteable() {
  4. return $this->morphTo();
  5. }
  6. }
  7. class Expense extends Model
  8. {
  9. public function notes()
  10. {
  11. return $this->morphMany(Note::class, 'noteable');
  12. }
  13. }
  14. class Review extends Model
  15. {
  16. public function notes()
  17. {
  18. return $this->morphMany(Note::class, 'noteable');
  19. }
  20. public function repairs()
  21. {
  22. return $this->belongsToMany(Repair::class);
  23. }
  24. }
  25. class Repair extends Model
  26. {
  27. public function reviews()
  28. {
  29. return $this->belongsToMany(Review::class);
  30. }
  31. }

as you can see Note is in the relationship one to many polymorphic and Reviews is in many to many with Repairs.

I would like to take all Notes that Reviews also has Repairs. How do I do such an operation?

Following the documentation I am trying to do something like this:

  1. $notes = App\Note::query()
  2. ->with(['noteable' => function (MorphTo $morphTo) {
  3. $morphTo->morphWith([
  4. Review::class => ['repairs']
  5. ]);
  6. }])->get();

I would like to my query should return something like:

  1. [
  2. {
  3. "id": 11,
  4. "noteable_id": 4,
  5. "noteable_type": "App\\Expense",
  6. "noteable": {
  7. "id": 4,
  8. "name": "Expense",
  9. "category": "general"
  10. }
  11. },
  12. {
  13. "id": 13,
  14. "noteable_id": 5,
  15. "noteable_type": "App\\Review",
  16. "noteable": {
  17. "id": 5,
  18. "name": "Review 5",
  19. "mileage": 120000,
  20. "repairs": [..., ...] //what I need
  21. }
  22. }
  23. ]

答案1

得分: 1

To load your Repair models for your Reviews, try this:

如果要为您的“Review”加载“Repair”模型,请尝试以下操作:

if ($type == 'App\Review') {
$query->with('repairs')
}

EDIT:

我刚刚对“whereHasMorph”进行了更多的阅读,我认为我对它的作用有所误解。它在最基本的层面上是一个“where”子句,旨在限制查询的结果。我上面建议的相当于将某些内容连接到MySQL子查询,这不是我们想要的!

您实际上想要做的是:

嵌套预加载多态关系

(https://laravel.com/docs/6.x/eloquent-relationships#querying-polymorphic-relationships)

英文:

To load your Repair models for your Reviews, try this:

  1. if ($type == 'App\Review') {
  2. $query->with('repairs')
  3. }

EDIT :

I've just done some more reading on whereHasMorph and I think i was mistaken as to what it is doing. It is at it's most basic level a where clause, designed to constrain the results of the query. What I have suggested above would the the equivalent of joining something to a MySQL subquery. Not what we want!

What you want to do is actually under

> Nested Eager Loading morphTo Relationships

(https://laravel.com/docs/6.x/eloquent-relationships#querying-polymorphic-relationships)

huangapple
  • 本文由 发表于 2020年1月3日 22:51:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定