我期待在laravel中减少我的查询大小。减少laravel中的数据库查询大小
我的查询看起来是这样的(我缩短了它,它是这一数额线的10倍左右):
$users = User::where("interface_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', $unavailableCheck)
->orWhere("interface_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', 1)
->orWhere("web_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', $unavailableCheck)
->orWhere("web_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', 1)
->orWhere("illustration_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', $unavailableCheck)
->orWhere("illustration_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', 1)
->orWhere("brush_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', $unavailableCheck)
->orWhere("brush_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', 1)
->orWhere("typography_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', $unavailableCheck)
->orWhere("typography_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', 1)
->orWhere("identity_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', $unavailableCheck)
->orWhere("identity_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', 1)
->orWhere("vector_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', $unavailableCheck)
->orWhere("vector_art", '=', 1)->where('role', '=', 2)->where('commstatus', '=', 1)
->orderBy($orderByString, 'desc')
->paginate(1);
正如你可以看到,这是一个有点多余。
对于每种艺术类型,如果它们的commstatus等于“1”或“$ unavailable”,我希望通过“2”角色获取用户。起初,我试图通过在每个“where”子句末尾添加“role”或“commstatus”来缩短它,并在底部编写另一个$ users = $ users :: where(“role” ,“=”,“2”),但我似乎无法找到正确的语法。
有什么办法可以缩短这个查询吗?
它看起来就像你有一个违反[零,一个或无限规则](http://en.wikipedia.org/wiki/Zero_one_infinity_rule)的模式,需要进行重构以具有某种程度的[数据规范化](http:// en。 wikipedia.org/wiki/Database_normalization)。使用正确的模式,这个查询可能非常简单。 – tadman
嗯,它工作得很好,我只是想缩短它,我似乎无法找到有效的语法。我试过在[documentation](https://laravel.com/docs/5.2/queries)中找到可能对我有用的东西,但我似乎无法找到任何东西。 –
我希望'$ unavailableCheck'不代表NULL。如果是这样,你还有其他麻烦。 –