Laravel 6 Beginner
Lesson 12 of 14 · Laravel 6 Beginner – e12 – MySQL Where Clause with Eloquent · Education
Laravel 6 Beginner – e12 – MySQL Where Clause with Eloquent
[Music] the eloquent ORM is what laravel actually uses to communicate with your database we have already touched briefly
on it and we've been using it along obviously doing something like customer all most of the time though you're going to be able to restrict what you're actually bringing back into your index
view when we explore some of the ways that we can add some where clauses if you are familiar with database development then you know that most of the time you're not going to retrieve
all of your customers all at once let's jump into PHP artisan tinkerer PHP artisan tinker is just a way for us to interact with our application without actually being inside of our application
inside of here each line will be as if you had written code line by line so we could say customers equals we'll have to do the full name space will say app customer Oh
and right now in our database we have three customers so there's several ways that we can restrict this we can say okay I want the customer where the ID is equal to one so
we could say customer equals app customer then we could say where and then what column are we looking for we're looking in the ID column where the ID is equal to one now at this point we
would actually need to fetch the record so we need to instruct Louisville to go ahead and perform the query because what could happen is that you could actually continue to chain other where statements
from here to tell level that you are done then you say get and now we have the customer with ID of one this is similar to what we did in the show view in our controller except
that because we are using route model binding then we are not actually doing it manually but we're letting laravel take care of it for us another thing you could do though is say well what about
other columns well surely you could say where the name column is equal to maybe Victor and that will return the exact same result and it's that one right there
let's add the concept of active customers and inactive customers into our application and let's see what that would look like let me exit out a tinker go back in here and the first thing we
need to modify is our migration so this is one tricky bit about migrations every time you change something in the migration it will not reflect in your database right away you need to actually
rerun the migration but doing so erases your entire database this is okay during development you're going to make a lot of changes to your migrations throughout development and the thing about it
though is that it will erase your database every time you run all of the migrations which isn't great and there are some workarounds that you could do like factories and we'll talk about
those later down the road but just know that it's okay to modify your migrations yes you will lose your records but there is no sense in recreating a new migration to add a field when in fact
you haven't deployed your code once your code has been deployed to a server though of course you don't want to lose your database so what you would actually do is instead of changing a current
migration you're going to create a new migration that does the modifications that you need right now this is okay because we have not deployed any code so we'll add a new one here will say table
and we can do this in one of two ways we can either store it as an integer or you can even store it as a string but just know integers are a lot quicker for MySQL to be able to grasp into and sort
by and things like that but let's use that and so let's use an unsigned integer not big into your just integer the unsigned integer and let's call this column active I'm
gonna teach you something else here if you need a default you can say default right after that and we'll say okay default will be one imagining that one is equal to active and then 0 is equal
to inactive so by default any customer you add will simply be active and that's okay then we can filter on only active records ok let's see what that would look like like I said we need to go
ahead and rerun this migration and we can run that with PHP artisan migrate and then there's a couple of different ones but the one that you will use the most often is just fresh fresh erases
the database it truncates every single table and then reruns your migrations and there we go so now when we go back to our project all of our records are gone but now whenever we add a new
record we'll say new pool customer email i'll email comm and add a new customer let's go back in to tinker and let's see what we have now PHP artisan
tinker let's go ahead and fetch that one record with ID of one we have an active column with one so pretty cool stuff let's do something let
me add yet another customer here then we'll do tests at tests calm okay so now we've got two customers so there we go okay so here's what I can do let me grab that first customer again
now this is going to return a collection notice here that we have a collection of records and then inside of that is where we have our array one thing you can do here when you're doing where and you're
only expecting one record is instead of gets you can run first and that will be a slightly different result and we'll talk about it just now so we'll do first now first well this
seems basically about the same thing the biggest difference here is that this is returning a single customer it's returning a model right remember a model it's just a representation of a row in
your database this one you get a collection and a collection in very simple terms is simply just a fancy array think of a collection as just a fancy array with added benefits you'll
learn to love collections as you start to use level because they are very powerful but it's not as easy to get the underlying objects as it is whenever we get the app customer so on this app
customer that I have I can make actual changes to it we'll say active let's set that equal to zero and then we'll save the customer will say customer safe
that's true so now let's fetch all of our customers we've got one do one is active of zero and the other one is active of one okay so now I only want to display active customers in this list
let's make the necessary changes in our customer control remember we are hitting the index method is the method here that displays all of our customers we are no longer going to use all but let's create
that same thing we had in Tinker we'll say where the active column is one and then remember we need to let level now go ahead and fetch these customers we do that with the method of get
alright let's give that another girl I'll hit refresh now sure enough another one is the only one that shows up and another one is this one right here it does have active let's revert the change
just to show that everything is working I'll grab that one record again and we'll say customer active equals one let's go ahead and save that customer ok refresh there we go now we have two of
our customers there's a lot to where statements and things like that in eloquent but throughout this lesson we start to understand the concept of how you would actually use things like where
clauses in your database such as the difference that we saw between a collection and when you have an actual record just to reiterate because this is a very important point that I think a
lot of people starting out will elbow get a little bit confused about they may think that a collection is a record in the database and it's not again a collection it's just a fancy array most
of the time you will use get and you will be inside a collection and in that point if you think of it as an array you need to be able to iterate through each of the records you can't just simply go
into the first record and change something but rather you would do a change to probably all the records if you only mean one particular record in that case you can call first not only
will this actually give you the exact record you're looking for but it will actually return you the model itself not a collection that contains the model inside that's the key difference there
so that's it that is the basic difference between get and first and how to add a wear statement to your eloquent calls
When fetching records from the database, most of the time you need to grab a subset of records and not your entire database. This is done using a where clause. Let’s add that to our app in this lesson.
For the best experience, follow along in our interactive school at https://www.coderstape.com
Laravel RESTful Controllers
https://laravel.com/docs/6.x/controllers#resource-controllers
DigitalOcean Referral
https://m.do.co/c/7dce5364ef4d
About This Course
If you have been looking to learn Laravel, there hasn’t been a better time. The framework reached an important point in its life, with the release of Laravel 6. There has never been more demand for Laravel developers than now. In this series, we are going to get you started in your path to becoming a Laravel Artisan. Let’s get right to it!