Laravel 5.8 – From The Ground Up
Lección 12 de 52 · Laravel 5.8 Tutorial From Scratch – e12 – Eloquent Where Clause · Educación
Laravel 5.8 Tutorial From Scratch – e12 – Eloquent Where Clause
now that we have a way of adding customers I want to be able to filter by active and inactive customers another popular thing to do with a customer list
in this episode we're going to be refactoring our code to accept an active column that way we can select if a customer is active or inactive and for now we're gonna do that on creation but
as with any project you can always add that later to an edit menu if you wanted to change the status of a customer but we are just gonna keep it simple so here's what I'm thinking I want to add a
third row here and it's gonna be a drop-down menu and then I want to have two lists down here my left list is gonna be my active customers list and my right list is gonna be my in active
customers list so let's start by modifying our view let's go to phpstorm and let's look at our customers view so here we are in our view right before the add customer let's add a new div with a
form group and let's add a select and the name of our select is going to be active we'll keep ideas active active as well and let's add an option options gonna be
empty and we'll just say disabled let's put a label on this select customer status let's add another option it's gonna be a one for active and let's add inactive and we'll give that a value of
zero alright so this needs a class class a form control let's check out the browser there we go so we have a nice drop-down and we can select active or inactive and let's add a label so label
it's for active will say status but right now this doesn't do anything let's start with our migration let's go to they create customers table migration and we need to add a new column here so
it's not gonna be a string let's use an integer and we'll call it active now that we've modified our migration we need to roll back the database and roll it forward again so let's do that now
PHP artisan migrate roll back so that deletes the customers table let's go ahead run PHP artisan migrate so now we have our customers table again with that of course we lost all of our customers
but at this point if we tried to add a new customer it would not save the customer status and that is because we haven't changed our controller let's go to our controller customers controller
and right down here so we are assigning a name and email and we need to assign a status so say active and that will come from our requests which means that we need to make that also required
we'll say active is required all right let's give this a run let's see what happens so our name would be test name our email will be something dot-com and we'll give it a status of active add
customer all right so our customer got added let's add an inactive customer now who said that to inactive add customer right now we are fetching all of our customers so they all get put in this
left list but like I said I actually wanted two columns I want to call them for active customers and I want to call them for inactive customers so let's go back to my customers blade and scroll
down and here's the section where we are echoing out each of our customers so one thing you'll notice is we have this customers variable here in our view and if you recall we are fetching that from
our database right here in the list menu we have this customers variable and we are simply fetching all of our customers but that's no longer going to work because we need to fetch our
inactive customers and we need to fetch our active customers so let's modify our controller to do that now so we'll call them active customers equals customer and the way you do this in eloquent is
we need to add a where clause so our active customers are where the active column is set to one and then get me all of the results does that make sense
so to fetch our active customers we're reaching into our customers table and saying where the active column is set to one give me all the results so let's do the inverse now inactive customers come
from customer where the active column is set to zero and get me all of the results for that just to show you I'm gonna die and dump the active customers list let's go back to the browser hit
refresh so there we are so we have one entry make sense we just have our entry that was the test name entry and notice that the active column is set to one let's die and dump the inactive
customers hit refresh we have one as well and we dive into the attributes we see that the active is equal to zero now we have two variables we have active customers and inactive customers so
let's get rid of this customer all call because that no longer applies and now we need to send both of these variables to our view let's do it in the long form way so active customers equals active
customers and then in active customers we'll set that equal to inactive customers there we go so now we need to modify our view let's change our column to six so that way we can have two
columns equal size so we'll say for each active customers as active customer let me go ahead and modify this as well
active customer so for each of those we'll go ahead and echo out the same we've been doing and let me duplicate this same thing and we're going to modify it from active to inactive so we
have our first iteration here through active customers and then we do the same exact thing through our inactive customers finally let's just add a label here
we'll say h3 active customers and we'll add another h3 here in active customers let's check out what we have and there we are so now we have a nice list of active customers and we have a list of
inactive customers let's add a new active customer here just to prove that it's working we'll say another person with a random email he's an active customer so that gets put in the active
column and let's add an inactive customer inactive add customer and there we go so we are properly adding active customers and inactive customers using that new column that we added to the
database one last thing I want to show you I want to show you a shorthand notation for passing data into views and you will probably end up using this most of the time there is a function called
compact and what compact will do is this exact same operation notice that active customers is referencing an active customers variable and then we have inactive customers and that references
an inactive customers variable so this is very redundant so we can get rid of all of this and we can say compact and all you're gonna pass in is just the name of the variable so active customers
and that's the second argument in active customers much cleaner way so this is doing the exact same thing that we were doing before we hit refresh and so we see that we
the exact same thing let's recap the entire episode so the first thing we did was we went into our view and we added a status column we did that using a select which is just a drop down menu and we
have active which is a value of 1 and inactive which is a value of 0 so then we went to our migration remember the migration describes the database so whenever we're going to do any changes
to our database we need to go ahead and change a migration just as a recap on that migrations only roll forward so if you have some code in production you would have made a new migration which
modified the customers table and added the integer active however we are still in development and during the development process you can change your migrations as many times as you want do
know that when you do and you roll back you're going to lose any test data that is in your database we will look at database seders later on which is a very convenient way of seating test data into
a database so for now you will simply lose your data you'll have to add your data back in just keep that in mind as a side note we added our integer column with a name of active so in our
controller in our store method remember this is the method that we hit whenever we hit this button right here we are targeting this method right here the first thing we did was make active
required that makes sense without having anything for active it doesn't quite work our query would blow up because it requires an active column so after doing that then we are setting our active
column in our customer to be that request active so we return back which means that we go to our list and what I wanted to do was have two columns I wanted to separate my active customers
from my inactive customers that way we can show that we were actually storing that variable so what I did was I split up into two variables now we have this active customers which is fetching into
our customer and saying we're active that Colin that we just talked about equals to one meet all of the results and save them to active customers and then we do the
inverse we say inactive customers equals go into my customers and wherever the active column is set to zero get me all of those results and then we're gonna return our view and we're passing our
data in this new compact way the compact function does the exact same thing we've been doing of assigning active customers equal to active customers as an array and inactive customers equal to inactive
customers the difference is this is just a shorthand notation for doing that so you don't have all of that repetition but great job so far if you've been following along go ahead and make these
changes to your project now
In this episode, we are adding a column for our customers list to make them active and inactive. Follow along as we add a where clause to our eloquent call to make this functionality work.
For the best experience, follow along in our interactive school at https://www.coderstape.com
Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch
Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)
About This Course
Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!