Laravel 6 Tutorial for Beginners #15 - Eloquent Models

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay then gang so far we have created a database and we've hooked that up to our laravel app and we've used migrations to create tables in that database now ultimately we want to store data in those tables for example in the pizza table that we created we want to store Pizza records and then we want to be able to interact easily with that date in our code and do things like get data save data update data etc now if we wanted to if we have the inclination we could use sequel commands to do it manually but in a laravel there's an easier way to do it and that is by using what's known as eloquent models but first of all what is a model well a model in programming is normally a class or an object blueprint that represents a certain type of data for example our pizzas table right here is going to store a certain type of data a pizza record which has a type property a base property a toppings property now in laravel we can create an eloquent model to represent that type of data a pizza model and then if we want to interact with that type of data in the database we'll use that model to do so now under the hood eloquent uses sequel to interact with the database but it also wraps all of that in an extra layer and it provides us with a bunch of easy to use methods on the model to read and write to the database so it's intended to abstract away from complex sequel code and instead provide us with a simple programmatic interface with simple methods we can use instead like find and save and get so in a way you can think of a model in laravel as a code representation for a specific table in our database so we'd have a pizza model like this to represent a pizzas table and if we had another table to store something like articles then we'd make an article model to represent that table and if we want to interact with the pizzas table to do something like save a new record or retrieve records we'd use the Pizza model to do that on one of these methods and if we want to interact with the article table with use the article model to do that so hopefully now you've got a basic idea of what an eloquent model is let's try creating one in our project so the first thing I'm gonna do is manually add a few records to our pizzas table and I'm doing it manually through phpMyAdmin because we've not yet learned how to do it programmatically using eloquent models we will see all of that later but for now so we have some data to play with I'm gonna add it manually so I clicked on this pizza's table right here then I'm gonna go to inserts and I'm not going to bother with the first few columns but I'm going to come down to type and by the way I took off the price column we don't need that in so the type of this one is going to be Hawaiian and then the base is gonna be cheesy crust and then the name the person ordering Shawn the second one down here we'll leave the ID created out and updated at fields but then the type is going to be Hawaiian again this time it's going to be garlic crust I cannot spell gar like crust and then final it the name is gonna be Mario and then finally I'll click on go right here so that should hopefully insert those two rows if I go to browse we can now see those right here now I'm gonna add one more I think so let me go to insert again and let's come to type and this one is going to be a volcano pizza and then the base is gonna be thin and crispy and this is gonna be for Yoshi all right so let's press go there and insert that one so let's go to browse again and now we can see we have these three records here so now we can start to play around with these records from our code so imagine now that in our project we want to interact with this table and this data well the easiest way to do that would be to make a new eloquent model for this table pizzas and then that model would come baked with other methods that we need to interact with this table and the data inside it so the question then is how do we create an eloquent model well again we can use our T's and to do that so down here I'm going to say PHP Suzanne and we use the make command and then this time it's a model and I'm gonna call this pizza with a capital P that's just a convention so I'm going to press ENTER and this makes us a model and that model is inside the app folder up here at the top you can see right here we have pizza PHP we already also have a user dot PHP model that comes baked with a laravel and it makes it automatically for every new project you create but this pizza one we just create it so this is a very very simple file just a class called pizza which extends model so it inherits all of the methods that we need to interact with the database table from this model now laravel automatically hooks it up to the pizzas table and it does that by pluralizing whatever the model is called but if you want to override what table it connects to you can do so inside this class and we do so by using a protected property called table and we set that equal to the name of the table that we runs a kinetic so in this case it would try and find a table called some name now we don't need to do that because it's going to automatically connect to the pizza's table for us and based on this name so now this model even though we've not really done anything to it is ready to use and we can use it now to interact with this table to get the data so let's try doing that so remember if we go to our routes file if you go to forward slash pizzas then the Pizza controller is handling this and we're using the index action and over in the controller this index action currently makes up a load of data and sent it into the pizzas view right here so how about now instead of making up this data we reach out to the database using that new model that we just created and retrieve all of the data and then pass that into the view instead so the first thing we need to do is actually use that new model inside this file so we can say use and then it's app back slash pizza that means we can now use this pizza model inside this controller so now down here all right delete all of this junk we don't want that anymore and instead we want to interact with the database using this Pizza model so we're going to store the results inside some kind of variable I'm going to call that pizzas and I'm going to set it equal to pizza which is the model right here and we can use a method so double cola called all so this method comes automatically on all models that we create doesn't matter that it's not defined inside our pizza model right here it's automatically inherited from this model class okay so this right here is going to look at our pizzas table because it's already hooked up to the pizzas table in the background and it's going to retrieve all of the different records inside that table and now they're going to be stored inside this pizzas a variable so now that is a collection of Pizza records so we're still passing them into this view right here we don't need to change that but we do need to go into the view so let me do that open up resources and then views and then pizzas which is the view we're using so inside here we've still got all of this loop from when we learn about loops we can keep the loop but I'm going to delete everything pretty much inside it except for that div tag so now what I'd like to do is output say the name and the type and the base of each pizza so remember to do that we use double curly braces to output a variable we're still cycling through the pizzas and without putting this for each individual pizza right here and this now refers to a single pizza record so before when we made up the data ourself each pizza was a pizza array now it's a pizza object from the database so what I can do is say pizza and then use from that the name property and that gets us the name because we have a name column okay so let's go back over here after that let's output the type so I'm going to say pizza and then type like so and then after that I'm gonna output pizza and then the base like so all right then so hopefully this should work I'm going to save this I'm also going to save this as well and then I'm gonna go to our project I'm gonna refresh to make sure the server is still running yes it is I'm gonna go to forward slash pizzas now and see if this works and it does we get all of this data from the database and now outputting it to our view how simple was that using this new model we just created ok so there are other ways to get the data from the database so let me just go through a couple of them for you so again let's say pizzas this time is equal to the pizza model and then we'll use a method called order by and you can probably guess what this does it orders the results based on a certain column so I can pass in a column name for example the name column and we're going to order the results based on that but this time when you don't use all you need to add on the get method as well because this just finds a selection in the database and orders them then we have to get them all automatically does that but if we use this we have to use gets on the end to get the results so this does pretty much the same thing but it orders them by the name now so if i refresh you might see these reorder a little bit and we get an error and that's because I've only added one : instead of two so let me save that and refresh and now we can see these are in alphabetical order for the name mas y now if we want to change the order we can add a second argument to say descending instead of ascending and then or refresh and it goes in reverse order cool and you could do this by any other column by the way it could be the base or the type that's absolutely fine okay so let's do the next one so we'll say pizzas again is going to be equal to pizza and this time we're going to use a method called where and what where does is allow us to select certain records from the table and based on a certain condition so I could say okay well get us all the pizza records where type is Hawaiian or the type is volcano or the name is Shawn and it gets us a collection of those pizzas that match that criteria so let's do that the first argument is what we want to match what column and that is going to be the type column and I'm gonna say okay get the pizzas where the type is Hawaiian so if we look in our database we can see that two of them are Hawaiian these two so it just gets this collection or right here but again it doesn't actually retrieve them we need to add on the get method to do that so this finds the selection for us this gets them so let me save that now and come over here and refresh we should only see these two right now and we do awesome okay so there's one more I'd like to show you and to be honest it's not going to have a big effect on how we view the pizzas right now but it will do later I just want to prep it now so pizzas is going to be equal to pizza and then use a method called latest and this orders the records that we retrieve from the database in date order so the most recent first of all now it's not going to have any effect on us because we don't have that field filled with anything yet but later on when we add these different records programmatically using the model those will be filled up and it will take effect for now it's just going to grab them all the same as this would do up here but let's just prep this for later so we need to say get again and this should basically just get us all of the pizzas so let's come over here and our refresh and we can see all of them again so there we go my friends that is how simple using these eloquent models really is and that is a lot easier than creating a load of sequel code ourselves so that's the basics of eloquent models in laravel and how we use them to interact with our database tables later on we'll be looking at how to use it to save new records to the database table as well but first of all I want to just take a little sidestep and go over a few naming conventions in allow Rafael before we start adding a load more file so we'll do that next
Info
Channel: The Net Ninja
Views: 56,336
Rating: undefined out of 5
Keywords: laravel, laravel 6, tutorial, laravel tutorial, laravel 6 tutorial, laravel tutorial for beginners, laravel 6 tutorial for beginners, laravel for beginners, laravel tips, beginners, laravel basics, laravel crash course, crash course, laravel models, laravel model, eloquent model, eloquent models, model, models, data model
Id: iaXtpAYfiy4
Channel Id: undefined
Length: 13min 10sec (790 seconds)
Published: Wed Feb 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.