禁用渴望的关系 [英] Disable eager relations
本文介绍了禁用渴望的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的项目中,我有许多雄辩的模型,它们在此类中配置了渴望的关系:
protected $with = [ 'countries', 'roles' ];
但是有时候我只需要没有任何关系的旧的普通模型.我能以某种方式做:
Model::noRelations()->all()
真的不想使用查询生成器,也不想仅在少数情况下创建另一个类.
解决方案
如果必须在模型上设置$with
属性而不是将其保留为空,则可以手动覆盖需要像这样急于加载的关系:
Model::setEagerLoads([])->get();
链接到setEagerLoads
的API /p>
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.
解决方案
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();
这篇关于禁用渴望的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文