禁用急切关系

扫码查看
本文介绍了禁用急切关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有许多 Eloquent 模型,它们在类中配置了如下的急切关系:

In my project I have many Eloquent models that have eager relations configured in class like this:

protected $with = [ 'countries', 'roles' ];

但有时我只需要没有任何关系的旧普通模型.我能以某种方式做:

But sometimes I need just old plain model without any relations. Can I somehow do:

Model::noRelations()->all()

真的不想使用查询构建器,也不想只在少数情况下创建另一个类.

Really don't wanna use query builder nor create another class just for few occasions.

推荐答案

如果您必须在模型上设置 $with 属性而不是将其留空,您可以手动覆盖需要的关系像这样渴望加载:

If you have to set the $with property on your model rather than leaving it empty, you can manually override the relationships that need to be eager loaded like this:

Model::setEagerLoads([])->get();

链接到 setEagerLoads 的 API

这篇关于禁用急切关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 22:41
查看更多