简体   繁体   English

如何在 Laravel Eloquent 中急切加载时获取多态关系类型?

[英]How to get polymorphic relationship type while eager loading in Laravel Eloquent?

While querying polymorphic relationship existence or absence, Laravel Eloquent provides whereHasMorph and whereDoesntHaveMorph methods which really helpful to create queries related to the types.在查询多态关系存在或不存在时,Laravel Eloquent 提供了whereHasMorphwhereDoesntHaveMorph方法,这对创建与类型相关的查询非常有帮助。 But what about for eager loads?但是对于急切的负载呢?

Here is the situation:情况如下:

Think about 3 models App\Photos , App\Videos and App\Blogs .考虑 3 个模型App\PhotosApp\VideosApp\Blogs

These three models are all feedable which means they have a polymorphic relationship with App\Feed model.这三个模型都是可馈送的,这意味着它们与App\Feed model 具有多态关系。

But while App\Photos and App\Videos models are reactable (have a polymorphic relationship with App\Reactions model), App\Blogs model doesn't have this relationship.但是,虽然App\PhotosApp\Videos模型是可反应的(与App\Reactions模型具有多态关系),但App\Blogs model 没有这种关系。

Let's check out the methods:我们来看看方法:

App\Feed应用\饲料

public function feedable() {
    return $this->morphTo();
}

App\Reactions应用\反应

public function reactable() {
    return $this->morphTo();
}

App\Photos应用\照片

public function feed() {
    return $this->morphOne('App\Feed', 'feedable');
}

public function reactions() {
    return $this->morphMany('App\Reactions', 'reactable');
}

App\Videos应用\视频

public function feed() {
    return $this->morphOne('App\Feed', 'feedable');
}

public function reactions() {
    return $this->morphMany('App\Reactions', 'reactable');
}

App\Blogs应用\博客

public function feed() {
    return $this->morphOne('App\Feed', 'feedable');
}

While querying feeds from App\Feed model, I also want to get reactions count.在从App\Feed model 查询提要时,我还想获得反应计数。 But because of that App\Blogs model doesn't have a method named reactions , this query causes errors.但由于App\Blogs model 没有名为reactions的方法,此查询会导致错误。

How can I create query like this:如何创建这样的查询:

$feeds = Feed::with([
    'feedable' => function($query) {
        // I need to get the feedable_type of the feedable here.
        // module_exists is a custom helper to check if an module is installed and active
        if ($type != 'App\Blogs' && module_exists('Reactions')) {
            $query->withCount('reactions');
        }
    }
])->all();

Edit编辑

I have tried to simplify my question but I think I need to provide more information about the application.我试图简化我的问题,但我认为我需要提供有关该应用程序的更多信息。

The application is a content management system (social network system) that third party developers can develop modules for.该应用程序是第三方开发人员可以为其开发模块的内容管理系统(社交网络系统)。 Blogs, Photos, Videos, Feed, Reactions, Comments, Share etc. are all different modules and they can be disabled or removed by the site owners.博客、照片、视频、Feed、反应、评论、分享等都是不同的模块,网站所有者可以禁用或删除它们。 So while developers are creating modules, they have to take into consideration that Reactions, Coments etc modules can be disabled or removed.因此,当开发人员创建模块时,他们必须考虑到反应、评论等模块可以被禁用或删除。

1- Naming conventions in Laravel states that models are singular form, so you may consider renaming the models to Photo and Video etc.. 1- Laravel 中的命名约定指出模型是单数形式,因此您可以考虑将模型重命名为PhotoVideo等。

2- In your case, the Blog model doesn't have reactions, so you may create a type Scope in the Feed model like: 2-在您的情况下, Blog model 没有反应,因此您可以在Feed model 中创建type Scope ,例如:

public function scopeHasReactions($query)
{
    return $query->whereIn('reactable_type', ['App\Photo', 'App\Video']);
}

3- use $withCount property to get counts, in Photo model in example: 3- 使用$withCount属性获取计数,例如Photo model:

protected $withCount = ['reactions'];

Finally you can make a query like this:最后,您可以进行如下查询:

$feeds = Feed::hasReactions()->get();

$feeds->first()->reactions_count;

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM