PHP Laravel Tutorial for Beginners 15 - Model Relationships


 hey what's up guys welcome back to our blog and welcome to this blog so this is part 15 of our larval for beginners tutorial and in this blog we're going to add or we're going to discuss the modal relationships so in the previous blog we've worked with the user ID so where we added the user ID to our database and the I think it's from the post so we've added our user underscore ID by using the migration and in this blog we're going to add the relationships relationship to this user ID to the actual users ID here in this table so that will enable us to only limit the blog's or the posts that are belonging to a certain user so so for example we're going to try to view the post so we all one or what we want is to only display the posts that are created by the logged in user in order for us to do that let's go back to our code editor and try to add the model in our models okay so let's open up our post modal so all we have to do here is to add a certain function so it is it is very simple to add relationships in laravel so as I've said all we have to do is add a function here so for example public function and then user and then return this and then belongs to the and then backslash user and the same thing with our user modal let's add a function here so public function and then post and then this should return has many relationship because a user can have many posts right so the user can post like thousands or hundreds of posts and let's go ahead open up our dashboard controller or home controller because that's where we were trying to display the post so in here we just need to get the user ID by using the oath and then user function and then supplying the ID or I think it's like that one instead of using it as parameter so user ID equals old and then user and then the ID attribute and then let's find the user fine and the user ID and then we need to pass in this user post into the home so that's squid and then close so it means that this post or this post are belonging to this political particular user supposed and comma and user post okay so if we go to the home home view so resources and then views and then home so after lagging in or after the user lag in so we need to display a table here that display or that will display all the posts belonging to the lag end-user so that's tabled and then class is table table stripe and then we're just going to define the headers or header for our table so that's eh and then I tell each and we're just going to add two other th here for our edit and delete button so th this is going to be just empty because this is going to be populated with ID or I'm in the edit and button edit and delete button so that's the H so in here I think you just need to check if the actual host at if and then count the post so if it is greater than 0 then we need to display the table right table and here we need to add the else and of course we need to say if the count of post is not greater than zero then we have to say you have no post no post and then and our if statement and if and of course in here we also need to populate the table rows so the title should be getting the title from the post so it's post and then the title of our post so there you go and then in here we just need to add the edit button first so a track and then the a track is post and then slash the post ID so that's basically post and then the ID and then we're just going to say slash Eddie and [Music] so just let's just add a class for this so class v TN v TN default before and then this should say it so let's try that refresh our page so let's go back to the home page refresh and then we need to log in so I think we forgot the user password let's try logging in so Tom Gmail that phone and then the password so log in we're just going to wait for a few seconds just to allow the log in so we have an error user not found in our home controller because I think we need to add that which one so home controller 27 home controller there you go so we need to add here the [Music] class so that's app and then backslash user okay let's try that save and refresh app user constant okay so I think we need to to remove the single code for that work and then refresh defined variable posts in home that laid that PHP so let's try to solve that home plain that PHP so it says here that we don't have access to the post for the actual post okay so I think we need to either it first in here to actually have the individual post so that's for each and then the post variable has single post and then let's just add the end of our for each here for each pair go so save and then refresh so we have here the new and updated view of our post so we can see the title which is post one post two and certain title of a post so let's try to add a new user so that in and then user 2 or make sure let's just say theme to solve and then team at gmail.com and then the password let's just apply a certain password okay and then register so as you can see the home or the location home it says you have no post because we just want the posts to show if the certain user lackin and then he has a or he has post so it should be displayed here but in case of our new user team so he has no posts or let's try to add a new post first post of team saw the body body of the post of teams so and then submit so this page displays all the post but if we try to go to the project application and then home it only displaced the first post or the single post out or a new user team saw so in the next blog we're going to discuss how to actually have the access control for this because we don't want the guests to actually create post and we just want the user to actually see his own post see you in the next blog thanks 

Post a Comment

Previous Post Next Post

Recent in Technology News