php - Reducing database query sizes in laravel -


i'm looking reduce query size in laravel.

my query looks (i shortened it, it's 10 times amount of lines):

$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);   

as can see, it's bit redundant.

for every art type, i'm looking users role of "2", if commstatus equal "1" or "$unavailable".

at first, tried shorten not adding "role" or "commstatus" @ end of each "where" clause, , @ bottom writing $users = $users::where("role", "=", "2"), example, can't seem able find right syntax that.

is there way shorten query?

as others have stated, schema benefit refactoring towards normalization. however, believe can still refactor existing query less redundant , more readable.

laravel has ability handle advanced clauses include nested parameter groupings. documentation:

the closure receive query builder instance can use set constraints should contained within parenthesis group.

with in mind, should able refactor query this:

$users = user::wherein('commstatus', [$unavailablecheck, 1])     ->where('role', 2)     ->where(function ($query) {         $query->where("interface_art", 1)             ->orwhere("web_art", 1)             ->orwhere("illustration_art", 1)             ->orwhere("brush_art", 1)             ->orwhere("typography_art", 1)             ->orwhere("identity_art", 1)             ->orwhere("vector_art", 1);     })     ->orderby($orderbystring, 'desc')     ->paginate(1); 

that create sql query does:

select * users      commstatus in ($unavailablecheck, 1)     , role = 2     , (interface_art = 1 or illustration_art = 1 or ... etc) 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -