hey guys in this video we're going to do another code review the project is about a small but
fairly well-structured blog api so i would say this developer is more of a mid-weight than a
complete junior so i'm going to show you three things that i would personally do different in
this project hey guys i'm dan and this is pixelfix and i'm here to hopefully help you improve your
code a little by pointing out some common mistakes developers tend to make if you like these kinds of
videos please hit that subscribe button and click on the notifications icon so you don't miss out
when i upload more of these and if you like this video or if you find it useful please remember
to hit the like button as well okay so let's jump straight in and look at the routes file
now if you look at the routes file you can see that there's some attempt made
to segregate the different sections which is not bad but i would just say in in this case it will
take an extra line so just put in a comment to say this is the auth section or we're looking at say
categories and over here let's say we're looking at tags and let's segregate this by putting in
posts i know this doesn't improve on any of the functionality but it makes your readability of
your routes fall somewhat better okay so something else i can see in this routes file is an active
post which sounds to me like an explicit route model binding so if we go to our route service
provider and scroll down to the boot method yep there we go it's an explicit right model binding
which basically returns the post it checks where the slug is the value and where it is active okay
so it basically just returns an active post okay so keeping that in mind let's quickly have a look
through the post controller and see what we've got all right so we've got the index method and
we've got a featured method which returns the posts where active okay let's have a look again
okay there's another one that's latest we're active
and as some another another repetition we're active in the gallery okay
let's have a look see if we can find another place where it's active
okay three places where i've seen this where active is now being replicated four places
so it's in the right model binding and then three of the methods okay so now a way around this code
duplication is to add a global scope within the model so if we go to our post model what we can
do is right at the bottom we can add a global scope now this is just good i copied straight
from the light of our website as i personally feel it's not something i'm going to learn off by heart
now let's look at the method as you can see this is the override method for the model booted then
what we do is we add a static global scope and then this is the name we're going to give it
so we can use the same name as previously used let's call it active post and then
in our query builder we can basically replicate what the active post was and it was active
equals and then let's just remove this and we can say it is true okay now back in our post
controller we can basically remove these where clauses in the featured latest and
gallery because your global scope will always be used first meaning that it will always return
active posts okay so the next thing we can do is in our route service provider is remove the
explicit route model binding for active post and then wherever it is referenced in the routes file
we can just replace it with the default right model bind of post
okay now back in our post model we can clean up this method a little bit better
by specifying a specific scope class so in our app let's create a folder called
scopes and then in scopes we create a new file called active post
scope dot php and we'll copy in some code straight from a lot of our website and let's just rename
this to our class because the class name and the file name should be the same so it's active
post scope and then basically what we can do is replace the query builder
with whatever we had in our model file okay so back in our model file we can basically replace
this whole section with new active scope active post scope and there we go this should work
exactly the same as before we've just moved the query builder into a specific scope class okay so
the next thing i want to show you is back in the api routes file if we look at the update method
on the post you can see it's using a batch method now back in the post controller under the update
method you'll see that one two three four the fields are required and only three of them
are nullable or not required which makes this method more of a
put method than a patch because it requires
majority of the fields okay so the next thing we can do is basically move this validation into
a specific validation class so let's create one called php artisan make request and let's call it
store post request okay then in our requests basically we've removed the rules and now
we're going to place the rules in the rules method and we can change this to true because we're not
doing any authorization in this request okay so let's look at the rules now in order for this to
be a proper batch method none of the fields should be required so we can use the sometimes keyword
on all of them sometimes
and some sometimes
sometimes
oops and sometimes on the image okay so let's clean up the rest of the rules
and make sure everything is working properly
and post can now be accessed via this post okay now back in our post controller we can ask laravel
to inject the store post request class instead of the default request i've actually seen we've
named it wrong so just let's just go ahead and rename it properly it shouldn't be store
post request it should be update post request so let's first change our name to update post request
and then in our class itself update post request and then back in the controller we call it up date
post request and in our import we just need to update it as well to update post request okay so
let's finish up our update method so we don't need the validator anymore so we can remove these lines
okay so this is where we're going to change our update process to be a little bit more patching
rather than putting and we can do this by creating special methods on the model in order to set the
attributes we'll also set it in such a way that we can chain it which would make our update process
a lot smoother and more fluent and it'll just look a lot better okay now in our post model i've set
up the methods in order to store or to set these attributes it's a simple set and then whatever the
attribute name is expecting a string value of that attribute and then setting the model attribute if
it exists and then it returns the model itself now this means that you can chain all these methods
together and i'll show you how to do it okay so back in our controller we can now take the post
which has been injected for us by laravel and now we can start chaining
the methods so we can say set title and then we're going to pass it the request
title and then our next method we can say set excerpt and that would also come from the request
okay so now we've chained all the methods and setting the attributes i'm calling a
special method commit which is a method i created in our model which just basically calls this.save
and then it returns the model itself now you can't call this.save on the chain because the
save method returns a boolean and you want the post to be returned because we are returning the
post inside the resource i've also gone ahead and set up a special method for our image attribute
which is called set image so now we can go ahead and basically remove this image we can also remove
the update because we're calling the save method inside the commit and we can also go ahead and
remove the fields array and now as you can see our update method is a lot smoother it's a lot
shorter and it is a lot more fluent that's it for this episode guys thank you so much for watching
i really appreciate it and i'll leave a link to the github repo in the description below where you
can see all the changes we made today if you liked this video and you found it useful please do me a
favor and give it a like also consider subscribing and hit that notifications icon then you'll be
instantly notified when i upload more content like this until the next episode guys see you later