in today's code review we're going to look at an inventory management system when you log in
you get a dashboard which displays summary data of your inventory it also shows recent movements
for those inventory if you click on sites you get a list of sites available within the system
same with users you'll get a list of users if you for instance search for a specific user
and view their details you can see all the inventory that's been assigned to this user
if you click on the inventory details you can then get a list of details for that piece of inventory
you'll also see an item history which is basically an audit log displaying the history of this item
hey guys welcome back i'm dan and this is pixelfix so let's jump straight in and
look at the go i always start off by looking at the routes fall because that's normally the
entry point for your application so in this case i can see this application has a lot of routes and
they are nicely segregated into different files which is something i would personally do myself
a big thing i've noticed is this application hasn't been updated in regards to the laravel
version as it still uses the level 7 way of importing or calling your controllers and
controller methods this way won't really work in any version above laravel 8. so the first thing
i would do in this case is upgrade my lateral version to the latest version 9 so i can utilize
the newer convention of calling your controllers by the controller name like inventory
controller class and then you call your method in this case it's not going to work yet because
i haven't upgraded a lot of our version and it's going to be too big a job for this video so we'll
just stick with using the old convention however something we can change in this route file is
if you look at the routes you can see all the routes are prefixed with the word inventory
so instead of applying it to each route we can apply it at the route group level
so we can call prefix here and name the inventory prefix and then we can delete
all the prefixed inventory like so now something else we can do in this routes file is we can see
that all the routes are calling the inventory controller again instead of applying this to
each route we can call it on the group level so we can say we can call the controller method
and we can pass in the controller which in this case would be inventory controller
and then we can remove it everywhere it's being referenced throughout the routes file
like so the last thing i would change within the routes file and the controllers is
renaming my methods to utilize the resource naming conventions if you're unsure about a
lot of all naming conventions i'll leave a link in the description to a video i've got
where it outlines the proper naming conventions within a lot of our project okay so the next
thing i want to show you is in the app when you search for a piece of inventory you get
export csv and export pdf options in the code they're referenced using the same
method within the inventory controller which is export search inventory and it differentiates
between the two using a query parameter called type okay so now i can see there's a bit of code
duplication going on here because it gets the inventory from the search in both the if and
the else statement so we can basically extract it and place it above the if statement like so
and remove it from the else okay so next i can see this ternary statement the if the search criteria
is null it gets assigned a default value which is basically something weird i'm not sure exactly
why that's being done okay so without changing the functionality of this method i will extract
the search criteria from the request like it's been done over here i will use a proper naming
convention which should be camel case instead of snake case like so let's fix up the variable
like so okay now as you can see the ternary statement checks the search criteria from
the request and if it's null it gets assigned a default value of abc so what we'll do is we'll
handle this assignment in our get inventory from search method so we'll say the default
value should be abc we should also tell laravel to expect that this search criteria variable could be
null so we would type in it the string and prefix it with a question mark which is basically the
null safe operator okay and now back in our export method we can basically remove this whole line
of code and the last thing we can change in this method is instead of using an else we can remove
this line and the else statement and just make that our default return the last thing i want to
show you in this section is back in the inventory from search method is basically changing the
variable follow standard like so and what else we can do is instead of assigning it to a variable
we can just return it directly and just fix up this code but the important thing i want to
show you is within this raw where statement if you use raw statements it's not a good idea to
inject your variables directly in the string but rather use parameterized queries like so by adding
your bindings as the second argument to your raw method and the reason we use parameterized
bindings or query strings is to prevent sql injection attacks a lot of our documentation also
states that when you use raw queries it's not 100 guaranteed to prevent sql injection attacks the
best is to try and avoid them as much as possible okay so before we move on to the last section
i want to ask if you enjoyed this video and you would like to see more of this kind of content do
you please hit that subscribe button now and also turn on the notifications okay so in this last
section we will look at the file upload method within the inventory controller within the app
this is basically the section where you can upload an image or a file or any kind of documentation
and associate it with each inventory item okay so the first thing what we'll do
is extracting our validation into a separate form request class so let's create one php
artisan make request and we'll call it store inventory file request actually before we do that
i think we should move our method into a separate controller because it is not really fitting inside
the inventory controller and it does it we cannot follow the proper naming conventions so
let's create a controller as well by php artisan make controller and we'll call it file controller
now what we can do is we can take our whole method and extract it into our file controller
like so and then we can rename it to store and what we need to do is just in our inventory file
rename that method to store as well okay so the next thing we need to do in this routes file with
regards to this route is to change the inventory to basically just inventory and then slug like
this so we can utilize laravel's right model binding now back in our controller method instead
of inventory slug we'll just receive inventory but we have to type hinted as inventory like so
then we can utilize route model bind right and now we can move our validation rules
into our form request and let's clean this up and say that's return type of bull
and we'll return true because we're not authorizing anything in this
form request turn an array and then we can paste our rules in like so and back in our controller
method we can remove these lines of code we can also remove this line because we can now
rely on laravel's route model binding and now we can ask laravel to inject our store file request
class instead of the default request okay so when you get to this part of your application
you can safely assume that your request will have a file called file because your form validation
would take care of that within your form request so this if statement to check whether the request
has a file can be totally optional now in order to save the file we can use the files relationship
that has been set up on the inventory model and we can do this by using the inventory and then
calling the files relationship and invoking the create method like so and then what we'll do is
we'll pass in the required fields which would be the description which will get from the request
and the file name which we'll get from the path info
and the file type which we'll also get from the request i will say it's the type
and we'll get it like so and then basically we can remove these lines of code as well just something
you need to remember is when you use the create method on a model these three fields that you
pass in needs to be in the fillable property of your model so we'll go into the file model and
we'll make it the description the name and type as fillable okay so just the last thing we need
to change is the inventory slug we can now say it is inventory and we'll call the slug property on
it there are still a lot of things we can change within this application but it'll just take too
long to do everything within this video so i want to thank you guys for watching i really appreciate
it and remember to reach out if you guys want me to do a code review or you are stuck with a
specific problem that you need some assistance on until the next episode guys see you later