Improve Controller: Shorter Eloquent with When()

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today i will try to improve eloquent query or in fact a controller based on this email in the attachment here or in fact in a full project we see this example in the controller so there's a query for querying the buildings from the database but the query is slightly different from the role it uses spotty laravel permission package and if the role is admin then the query is this if the role is supervisor then the query is similar but with a difference that checked at is introduced otherwise if the buildings are not yet set which means none of those roles are present then we query the buildings with user id and also cache that and the question was how to improve that to avoid three repeating queries so let's do it step by step first what are the absolute repeating parts and we can extract them outside of that so i've noticed that building select is absolutely identical and also when request keyword is also present in all those three rows so let's actually copy everything and also the repeating parts are order by order by and paginate those are absolutely identical so let's get them outside of those if so select is the same one is the same user not needed here and order by will still be present and the when syntax is actually really suitable here even for chaining the one so instead of if statements i would suggest to query when something else add more condition so this part stays identical for all the roles this part is identical and then we add rows here so instead of having if statements we would use when the role is admin then function query what we need to add for admin in fact there's no additional query for admin so we don't need to add anything here so we can completely remove that if statement because that is how admin works by default without this so this is a query for admin now let's go deeper supervisor supervisor adds checked at so let's introduce exactly that so one has row supervisor then we add query where this copy from here query where like this okay so we've done with the supervisor and we can remove that one as well so much shorter already right and now we get to the last part to get the buildings if it's not admin or supervisor and it adds this condition where user id so let's implement exactly that we copy and paste the one condition and we'll add when not auth user has role of admin or supervisor like this because has role function in spotty permission package if we dive deeper into the trade off has roles has roll function may be accepting rows as string so it may be string it may be integer it may be instance of row object or it may be array and array would trigger the for each loop of each of the rows so if i'm not mistaking this is the correct condition for not admin and not supervisor and then the query where would be query where user id equals user id like this and then we also need to pass user id here use user id and let me check if we pass the use elsewhere oh in fact this one i think this is incorrect because i'm not sure if the keyword is used elsewhere i don't think it is so probably a more accurate way would be to use request here and then pass request keyword here okay so now we have query with three conditions and that is much shorter isn't it so we can now remove all that if buildings except for one thing we didn't encounter caching so there should be cash forever on the case where it is not admin or supervisor so in this case how to implement that let's just copy and paste this condition so if not admin or supervisor we will do caching so if not we will do cash forever like this and also let's check if the cache exists before running this query because we may not need it if the cache exists at least that was the logic of previous query when i took it over so we get the buildings from the cache if not buildings then we run the query and then cache it if we have those roles so that is my version of improving that query what would you differently maybe i didn't encounter something and to be honest i didn't actually launch this query that was just my kind of live mode live code workaround so i hope i didn't break any logic but it seems logical to me maybe you have something to add then shoot in the comments and if you want to know more about such eloquent stuff like eloquent when or other eloquent functions i have a few courses on my teachable platform the newest one is for eloquent performance but i also have a course called eloquent expert level which is an older one but majority of those things are still relevant so you can get all those courses one by one you can purchase them and all their courses are actually cheaper nine dollars plus v80 but you can get all of them by purchasing a yearly membership for 99 plus taxes if that applies to you currently i'm working on a course about graphql which should be released by the end of november and see you guys in those courses or in other free videos on this youtube channel
Info
Channel: Laravel Daily
Views: 10,018
Rating: undefined out of 5
Keywords:
Id: lOHq3DSlI6E
Channel Id: undefined
Length: 5min 32sec (332 seconds)
Published: Sat Nov 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.