Laravel From Scratch [Part 10] - Model Relationships

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
alright guys so now we can create posts when we're logged in and it'll tell the database it'll update the database with the user ID so what I want to do now is add a relationship between the blog posts and the users and we want to be able to display just the current users posts in the dashboard all right then we'll move on to access control and things like that so what we'll do is open up visual studio code and let's go into the models ok so we want to go into the post model and creating a relationship is super easy all we have to do is create a function here so we're going to say public function and we're going to call it user and then all we have to do is say return this this belongs to and then the Malta the model of the user which would be AB slash user so what this is saying is that a post has a relationship with the user and it belongs to a user alright so a single post belongs to a user so let's save that and then on the other side of it we're going to go to the user model and we're going to create a function called posts and we're going to add the has many relationships or return this has many and then app / post okay so we're saying that a post or I'm sorry a user has many posts ok because the user can have more than one post so it's a one-to-many relationship so we'll save that and just adding these these two lines of code in these two files it makes us so that we can use certain certain queries and certain elements that normally wouldn't be available to us now in the dashboard we want to fetch the post for that specific user so let's go to our dashboard controller and in the index we're going to add some stuff here we're going to say user underscore ID equals and then we can get the the logged in users ID by doing this all right and then once we have that let's go ahead and say user equals and then the user model and we want to find by the user ID okay and then we're going to pass along to the dashboard we'll say with and here let's say posts and since we added that relationship now we can say user posts just like that which is very convenient so let's go ahead and save that and then we'll go to our dashboard view dashboard blade and what we're going to do here is create a table so it's a table class table and tables Striped okay and we want to have a table row with some headings so let's do TR and then here we'll do th okay all we're going to have is the title and then we're going to have two other headings which will be blank because that's going to be the edit and the delete buttons okay so now under here we're going to loop through all the posts for that specific user okay so for each clothes as host don't need that and then let's put I'll just copy that so this is going to be the post title which we can just get with post title and then we want the edit button so that's going to be an a tag so it's going to go to slash post slash in the knee ID / edit alright we'll give that a class of BTN and - BTN default so let's check that so far class HTTP control is you okay we got to bring in the user model into the dashboard controller so a POS a use app slash user property post does not exist on this collection instance what do you mean oh this is wrong this should be user and then ID with that and there we go so that's going to show all of this users post now what I'm going to do is log out and then register as a new user we'll just say test at test comm register let's see not found all right so I think that this is happening because there are no posts on so in the dashboard where we did our for each actually let's wrap it around the whole table so we're going to put if count and you'll say if count yeah posts is greater than zero then we want to show all this stuff let's try that no it's not it oh that's spelt wrong wait a minute okay so we're getting redirected to dashboard that's spelt wrong so login controller register right here spelt that wrong you guys probably didn't do that so it should be alright now now when we register it'll redirect us to the right place which will be the dashboard and notice that John Doe has no posts so in that that if statement in the template here let's go and let's say else you have no posts okay and we'll create a post let's call it post five this is post five by John actually we don't need to put by John okay and now if we go to our dashboard will only see post five because that's the only one that we created with this account and the edit button should work okay and now let's add a delete button so we're going to do we're going to take the same stuff that we have in the show view for the delete which is this form right here and then we'll bring it in to our dashboard view into this th right here actually these should be TVs not th's all right so let's paste this in here and that stuff should all be the same we'll save it and let's create another post just to delete okay so we'll go to our dashboard and let's delete and there we go so that works good now another thing I want to do is make it so that it says written on and then the date and then the name of the user and since we added a relationship we should be able to do that so let's go to our post index and go to right here we have written on and now we should be able to do will say by post and then we should be able to do user remember we added that user function with the relationship and then we can get any user field and we want to get the name alright so that should just automatically work so let's go back to posts and there it is so you can see it has the right user will also do it on the show page so let's just copy this and go to show Glade and we're going to put that right here so now both the index and the show should have the author's name alright so in the next video we're going to start getting into access control because we don't want guests to be able to post and edit posts we also don't want users to be able to edit each other's posts so we'll get into that next
Info
Channel: Traversy Media
Views: 180,240
Rating: undefined out of 5
Keywords: laravel, laravel blog, laravel database, laravel relationships, laravel models, database migrations
Id: 42l4nHl_aUM
Channel Id: undefined
Length: 10min 4sec (604 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.