0

再会,

我有点卡在这里使用 Laravel 范围和 Eloquent Polymorphic 一对多关系获取最新项目。

鉴于:

  • 我正在使用最新版本的 Laravel 6.x。
  • 我有两个模型:WebsiteStatus.
  • Status模型是可重复使用的,可以与其他模型一起使用。
  • 每个网站都有多个状态。
  • 每次更改状态时,都会在 DB 中创建一条新记录。
  • 活动网站状态是数据库中的最新状态。

网站型号:

class Website extends Model
{
    public function statuses()
    {
        return $this->morphMany(Statuses::class, 'stateable');
    }

    public function scopePending($query)
    {
        return $query->whereHas('statuses', function ($query) {
            $query->getByStatusCode('pending');
        });
    }
}

状态模型:

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

    public function scopeGetByStatusCode($query, $statusCode)
    {
        return $query->where('status_code', $statusCode)->latest()->limit(1);
    }
}

问题是当我打电话时:

Website::pending()->get();

范围将pending()返回所有已获得pending分配状态的网站,而不是当前处于活动pending状态(例如最新状态)的网站。

这是返回的查询DB::getQueryLog()

select * from `websites` 
    where exists 
    (
        select * from `statuses` 
        where `websites`.`id` = `statuses`.`stateable_id` 
        and `statuses`.`stateable_type` = "App\\Models\\Website" 
        and `status_code` = 'pending' 
        order by `created_at` desc limit 1
    ) 
and `websites`.`deleted_at` is null

pending使用具有多态一对多关系的范围获取网站的正确方法是什么?

此处描述了类似的问题:https ://laracasts.com/discuss/channels/eloquent/polymorphic-relations-and-scope

谢谢。

4

1 回答 1

0

好的,在做了一些研究之后,我偶然发现了这篇文章:https ://nullthoughts.com/development/2019/10/08/dynamic-scope-on-latest-relationship-in-laravel/

解决方案非常有说服力(对不起,双关语不好):

protected function scopePending($query)
{
    return $query->whereHas('statuses', function ($query) {
        $query->where('id', function ($sub) {
            $sub->from('statuses')
                ->selectRaw('max(id)')
                ->whereColumn('statuses.stateable_id', 'websites.id');
         })->where('status_code', 'pending');
    });
}
于 2019-12-07T14:36:13.650 回答