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

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

Laravel querying polymorphic relationships with nested many to many items

问题

以下是您要翻译的内容:

我有4个表格,它们之间有以下关系:
class Note extends Model
{
    public function noteable() {
        return $this->morphTo();
    }
}

class Expense extends Model
{
    public function notes()
    {
        return $this->morphMany(Note::class, 'noteable');
    }
}

class Review extends Model
{
    public function notes()
    {
        return $this->morphMany(Note::class, 'noteable');
    }

    public function repairs()
    {
        return $this->belongsToMany(Repair::class);
    }

}

class Repair extends Model
{
    public function reviews()
    {
        return $this->belongsToMany(Review::class);
    }
}
正如您所看到的,Note 是一对多多态关系,而 Reviews 与 Repairs 是多对多关系。

我想要获取所有与 Reviews 关联并且同时具有 Repairs 的 Notes。我该如何执行这样的操作?

根据文档,我尝试像这样做:
$notes = App\Note::query()
    ->with(['noteable' => function (MorphTo $morphTo) {
        $morphTo->morphWith([
            Review::class => ['repairs']
        ]);
    }])->get();
我希望我的查询应该返回类似以下的结果:
[

    {
        "id": 11,
        "noteable_id": 4,
        "noteable_type": "App\\Expense",
        "noteable": {
             "id": 4,
             "name": "Expense",
             "category": "general"
        }
    },
    {
        "id": 13,
        "noteable_id": 5,
        "noteable_type": "App\\Review",
        "noteable": {
             "id": 5,
             "name": "Review 5",
             "mileage": 120000,
             "repairs": [..., ...] // 我所需的内容
        }
    }

]
英文:

I have 4 tables with the following relationships:

class Note extends Model
{
    public function noteable() {
        return $this->morphTo();
    }
}

class Expense extends Model
{
    public function notes()
    {
        return $this->morphMany(Note::class, 'noteable');
    }
}

class Review extends Model
{
    public function notes()
    {
        return $this->morphMany(Note::class, 'noteable');
    }

    public function repairs()
    {
        return $this->belongsToMany(Repair::class);
    }

}

class Repair extends Model
{
    public function reviews()
    {
        return $this->belongsToMany(Review::class);
    }
}

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:

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

I would like to my query should return something like:

[

    {
        "id": 11,
        "noteable_id": 4,
        "noteable_type": "App\\Expense",
        "noteable": {
             "id": 4,
             "name": "Expense",
             "category": "general"
        }
    },
    {
        "id": 13,
        "noteable_id": 5,
        "noteable_type": "App\\Review",
        "noteable": {
             "id": 5,
             "name": "Review 5",
             "mileage": 120000,
             "repairs": [..., ...] //what I need
        }
    }

]

答案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:

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

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:

确定