Laravel From Scratch [Part 6] - Fetching Data With Eloquent

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back in the last video we create we did a few things we created a post controller and we added some resource functions here index create store etc we also created a route if we go to our routes web PHP file we created a resource route for our posts ok so that's going to automatically map routes to those functions and we also created the model if we look if we go to post PHP that's our model which currently doesn't have anything in it now your model really you don't really have to do much here because everything is in this ok all the you know when we do something like post the post model and then we do all ok to get all posts so this is this is coming from the the model that dis extends so we have a ton of functionality available to us already through the model ok so in here what we'll be doing actually isn't very much if we want we're going to be adding a relationship between posts and users later on so we will have to do that here but other than that we can set some values here like the table name so by default to the table if we if we create a model called post by default the table name is going to be posts plural but if you wanted to change that you could and then you would just have to specify it here by saying protected table equals whatever whatever you name the table ours is post so I'm just going to keep that we don't have to have this here but I'm just going to put it just for reference alright you can also change the primary key field so in our case it's ID actually let's do will be protected so protected primary key X I'm not sure this may have to be public let's just make it public and primary key for us is ID because that's that's the default and that's what we have in our database but if you wanted to change it to something like item ID or something like that you could do that you just have to specify it here all right you can also specify if you want timestamps for your records so we could say public timestamps and we're going to set it to true we don't have to have this because it's true by default we have the created at and updated at field but if we didn't want those we could put false here alright so those are just some of the things that you can do in your model and this is all in the documentation all right so let's just save that and we'll close it up for now and what we want to do is go to our controller so we're going to work on the index the posts index so if we go over to our application and we say slash posts that's actually mapping to the the index function in the post controller and the reason it's mapping to that is because we have our resource routes here remember in the last video we Iran that artisan command that showed you all the different routes so what we need to do here is we want to load the view so let's say return view and this is going to be in a folder called posts in the views folder and then a file called index blade PHP so let's go create that so resources views and then let's create a folder in here called posts and a file called index played dot PHP and then here we're going to extend our layout which is in layouts dot app okay because it's in the layouts folder and then in app blade PHP then we want to include the main section or the content section and here let's just let's just put an h3 and we'll just save post actually an h1 and we'll save clothes alright so if we save that and now we go to slash posts you'll see we it's loading that that template that view so back in post controller we want to fetch our posts so we're going to bring in the model okay now the model has the name space of app and then it has the title of post so all we have to do to bring it in is say use app back slash post and then we can simply say post and we can do we can use any of the model functions okay and this is actually using eloquent the the object relational mapper which makes it easier to do database queries rather than write out SQL but we can do a regular SQL and I'll show you how to do that in a second so let's put a double colon and then all okay and that's part of eloquent that's going to fetch all of the data in this model in this table so if we just test this out and return this okay it's going to return this and stop it's not going to load the view you'll see that it actually gets all of our posts okay it's in an array so let's go back and instead of returning it we'll put it into a variable and then we'll pass it into our view with with alright and then in our view in the index we're going to look through it so let's first check for posts will say if count posts is greater than one and I'm going to put an else and then an ENDIF so the else we'll just put some paragraphs and we'll say no posts found okay if there are posts then we want to loop through them so we'll do four each and we should be able to do posts as post and then here let's put a div with a class as well or using bootstrap and in here let's pull it in h3 and we'll just do double curly brace post title alright let's try that out and there we go so we're looping through the posts in our database and then let's just put a small tag underneath it and let's just say written on and then we'll do post created underscore at all right later on we'll add the author when we implement users but we haven't done that yet so right now we'll just do the date so let's add a link to our navbar to go to the blog so we'll go to our views in navbar blade and right here let's just copy this and we'll change this to blog and it's going to go to slash posts okay so next thing I want to do is I want to be able to click on this on one of these posts and have it take us to an individual page now when we created our resource methods there's one called show and that's what takes care of the individual the individual posts so let's go back actually let me close that one out alright so let's go back to actually let's add the link first so in our index will go inside the h3 here and let's put an a tag wrap that title and then this is going to go to slash posts slash and then the ID which we can easily get with post ID all right so let's save that and then it should take us to a blank page because it's just it's loading the show function but there's nothing there so let's go to our controller and go down to show and notice that it's passed in the ID it's going to get the ID from this URL so it'll know that this is post one this is post two and then to fetch it from the database all we have to do let's say return and we're going to do posts which is our model and then we're going to do fine and we're going to pass in ID and that's it he'll find it by the ID if we go and we click on one you'll see it loads post one over here returns post two so eloquent is very very simple it's very handy and it's very clean and I'm going to show you some of some of the other stuff we can do with with eloquent up in the index but I just want to get this settled first so we know that this returns the single post so let's put it into a variable and then we're going to load the view so we'll say return view we're going to return it's going to be in the post folder and then it's going to be in a template called show all right and then we're just going to pass along post and then the variable post and that's it so you can see how clean laravel is compared to something like CodeIgniter all right not to bash code ignitor it's just I think it's a good comparison because I just I did a series on it I do like it I think it's a great framework to learn MVC with it was actually the first PHP framework that I learned but I think laravel is much cleaner alright so now we have to create the show view so let's go to our post folder in the views and say new file show dot blade dot PHP and let's see we're going to do our extend I'm just going to copy this all right and then when I have an h1 and then an h3 ending it I got to install Emmet all right so let's go back to show and this should just be in h1 and we should have access to the post because it's not an array we don't have to loop through it so let's just say post title and then I'm just going to get rid of this and let's test that out so we'll go back and click on post 1 and you'll see that it gets the title ok very simple let's add well at our small tag here and we'll just say written written on post created at all right and then we'll put the body in so I guess we'll just put this in a div later on I'm going to show you how we can implement a WYSIWYG editor so that we can actually you know have a little section where we can choose headings and fonts and all that stuff and we'll actually save it as HTML but for now it's just going to be plain text so let's take a look at that and there we go we have the title the date and the body actually now I'm going to do I'm going to put the date underneath everything so let's take this copy it and then under the body let's put an HR and then we'll put the written on there we go we should probably put a back button as well so let's go worry above the h1 and we'll just put a link and I'll say go back and that's going to go to slash our / posts and let's add a class of BTN and then BTN default now we can go back alright so before I go I just want to show you some other things we can do with eloquence so let's go back up to the index where we're just selecting them all now let's say we wanted to order it by something let's say we want to order it by title so what we could do here is instead of using all we can say order by and then in here we want to say what we want to order it by which will say title and then the second parameter will be if it's going to be ascending or descending so let's say ASC and then just that isn't going to isn't going to get it we actually have to call get okay if we're going to add clauses like this then we're going to say get at the end so let's go ahead and save that reload okay and I guess if we do descends it should reverse so de SC and now if we reload you'll see post two comes first alright and that's actually how I want it because I want the most recent to be the first now if you wanted to get an individual post just like we did down in the show and obviously you can use find like we did but you can also can also go like this you could say Post equals the model and then we're and instead of the getting it just by the ID like we did below we could get it by title if we wanted so you could say we're a title is post two and we asked if we have to add get to that and let's just return that and you'll see that it's only getting us post to when it's getting it by the title it's using a where clause so we can do that as well I'm just going to comment these out or comment that out now what about not using eloquent let's say you want to just use SQL queries for that we can bring in the DB library by just doing use DB and then let's comment this out for a minute and let's say posts equals and then we're going to do DB and we're going to do a select and we're going to say in here we're going to put our query which will be select all from posts okay so that should work the same way it should get our posts let's reload and it does alright so you can use this if you want but I don't see very many cases where you should use this over eloquent I just think that eloquence a lot nicer and a lot easier so let's comment that out we'll put this back I actually want to put the initial query back as well just so you guys have it so post all like that and I'll just comment it out I should probably put this one lap down here next to the return all right that way you guys have this if you need it for reference make sure it works good now we can also limit our posts pretty easily so if we were to do let's see we'll just copy this and then just comment this one out for now and then let's just add on to this we're going to say take we can just change things like this which is nice so let's say take one and now if we go back and reload actually that's not right take oh I think I got to do equals one let's try that no that's not right what the hell because its water that's supposed to work let's return this and see what I get what it gives us oh so it did ID it's weird let's go back no post found I'm sure why it's doing that because we're putting in the variable we're passing it in it should be an array of one value what if we did take two huh oh wait a minute I did post greater than one it should be greater than zero that's why I wasn't showing because it was one it wasn't greater than one I don't know why I did that so now if we go back and say take one it should give us one good all right so I just want to show you you can do that and then the last thing we're going to look at is pagination and I'm just adding it because it's really quick and it's really really simple to do all we have to do is grab this and instead of get we're going to say paginate and we only have two posts so I'm going to paginate it with one per page so now if we go back there's still only one but we can put our pagination links down here very very easily we'll just go right into our index and go under the for each still in the if but under the for each and all we have to do here is say posts lengths and that's it watch this reload look at that they're even styled so post one we can go back to go to the next page obviously we want more than one per page so I'm going to just change that to let's say 10 and then once we hit 11 the pagination will kick in right now it's not going to be there because well we only have two posts all right so you can see how easy that is with laravel in the next video what I want to do is start to work on the form so that we can actually add posts through our application alright so sit tight I might have to upload the next video tomorrow so don't worry that will be the next thing that's uploaded I'm not going to go off and do something else and come back in a week so don't worry about that so let me know if you guys are liking the series and I will see you in the next video
Info
Channel: Traversy Media
Views: 348,450
Rating: undefined out of 5
Keywords: laravel, laravel eloquent, eloquent orm, orm, object relational mapper, laravel database
Id: emyIlJPxZr4
Channel Id: undefined
Length: 19min 30sec (1170 seconds)
Published: Sat Jun 03 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.