Part 31 – Blog Categories and Learning Relationships [How to Build a Blog with Laravel 5 Series]

hey everyone welcome back to how to build a blog with level this is part 31 of our super long series and this is taking absolute beginners from knowing nothing about level up to building a pretty cool advanced blog application now we’ve done a lot so far we just finished all of our authentication and I think you guys are hopefully you guys are really happy with what we were able what we were able to do where with our authentication we kept it simple and I know that but it is a very powerful authentication system with all the basic features you need to get up and running so hopefully you found that useful again I do promise that we will do more advanced authentication stuff in the future that’s going to come up as soon as I finish up this tutorial series so for this video part 31 we’re going to actually be working with relational databases so we’re going to be working on creating a categories system where we can attach categories to blog posts and we’ll be able to modify and manage our categories independently of our blog posts and we just link a blog post to a category okay and the way we do that is with relational databases we’re going to be learning a one-to-many relationship today and that’s why we’re going to be working with categories is to explore that one-to-many relationship and to learn how label handles those relationships inside of eloquent or inside of label and stuff like that so it’s going to be a good episode today guys I’ve got a ton of stuff so we’re going to be moving to very quickly there’s going to be lots of code I’ll have all the code available for download at the end of the video so you might want to watch this one over a couple times to get but without any further ado let’s go ahead you all right welcome back everybody today it’s all about relational databases this is a huge milestone for you guys first of all I want to say that if you’ve made it this far Congrats we have learned so many awesome things and these videos coming up we’re going to be moving a lot quicker because we can because you guys have already learned some of the basics of what how controllers work you know what models are you know how to create basic actions for control all that stuff the crud everything we’re going to be able to move a lot faster I’m not gonna have to reacquaint all those things and I’m just going to assume you watch the previous video if you haven’t click the description down below and you’re going to want to check out the playlist and you may want to start from the beginning anyway this video is it going to be a huge milestone in our learning because it’s going where to be learning an extremely powerful concept in any sort of any sort of web framework or web application and that’s working with relational databases ok we’ve been doing databases up to this point we learned how to create you know posts and stuff like that in our database we learned all that kind of stuff however what we haven’t learned is how to connect to different tables in a database together how can we get different tables to talk to each other and stuff like that and this is a very powerful concept that as we start getting into more advanced applications we’re going to be relying heavily on this we’re going to have more relationships than you know what to do with ok and so we’re going to talk a little bit about relationships today and we’re going to be working with one of these relationships to connect our categories that’s what this video is about and it you guys this is that you might need to rewind this video because it’s going to be jam-packed full of information just tons of information in it so get ready to learn in Word this the categories are going to be split into two videos this video today we’re actually going to create the migrations and the models for our category create a table in the database for it and then we’re also going to look at the relationships and actually connect posts and categories together through level and kind of see how that works and then the next video we’re actually going to create like a user interface where we can create new posts and we can edit delete them all that stuff the crud part and kind of do it more dynamically so we’re actually going to work on the user interface and in the next video and in this video we’re just going to be looking at the nuts and bolts of how it works okay so let’s go ahead and get started first things first we need to understand what relational databases are or what relationships in databases are so let’s take a look at that before we start coding and just diving right in alright now to understand how a little bit more about relational databases I want to show you guys kind of an example that I’ve created to maybe help you understand it a little bit better so you can kind of see what I’m talking about when I mean when I say the word

relationships okay so let’s take a look at what those what that means so what you can see here is that we’ve got a user’s table now in this table we can think about this as some sort of like an employee management system so we’ve got each user is going to have their job look their name obviously what their job is their job title and then the department that they work for all right so you can see here you can see here that we’ve got you know John Doe works in he’s a JavaScript developer and he works in the programming department Jane Smith is a PHP developer she also works in the programming department Alex Curtis here I used to be a marketing manager so I’m marketing manager I work in the marketing department and factory worker and then Daffy Duck is a factory worker working in the production department okay so this all seems really really good this seems like a good way to store information right and this is kind of equivalent to what we are doing right now we’re basically storing information in this manner we’ve got one database that we’ve created and then each of these columns represents a you know an item of information that we would store about somebody so in this case we’ve just got their job title and their department now we could add more columns and store more types of information so let’s take a look at how that might work if we were to expand that so here we’re taking the exact same information but we’ve got now we’re storing their manager and the location that they work at now in this example we’re assuming that the programming department works in a different location than maybe the marketing department and so forth but the thing that you’ll notice here is that there’s a lot of problem there’s a lot of repetition going on in this database for example everyone that works in the programming department is going to have Susan as a manager because she is the manager of the programming department everyone that works in the marketing departments going to have Alex as a manager and everyone in the production departments can have Tommy as a manager because they all work in that department and these are this other information that we’re collecting is relative to that department okay also everyone that works in the production departments going to work over at this address 987 best Avenue everyone working in the marketing departments going to work at this address and the programming department is all going to work at this address so it makes you wonder that as you start collecting tons and tons of employees here this is a simple example but as you start collecting more and more employees this there’s going to be a lot of repetition this causes problems as we start looking into things like what if let’s say Susan gets fired or replaced maybe she gets promoted and someone else becomes the manager of the programming department now you’ve got to go through and everybody that was working in the programming department you now have to go and make sure that you’ve updated their manager inside of the the users table and that becomes a huge headache for you to do it’d be a lot easier if you could just change it in one place and then that information would automatically propagate through to everyone that works in the programming department okay now this is a common scenario you run into with athlete’s web applications with almost any web application you’re going to run into these types of scenarios where you have information that you need to connect you get one piece of information to another but they because they’re different pieces of information you may need to still keep them separately and that’s where relationships come in I’m not quite – the relationships yet but let me show you another example how we can simplify what we’re doing here and that’s by actually pulling out all the department information into its own database table so what you see here is we’ve created now a departments database table this departments database table tells us that everyone working in the marketing department works in this address and has that manager everyone the programming department works here and has this manager and so forth okay so we’ve taken the same information we had before but now it’s contained in one information one place one table which makes it super easy so now with thumb we changed if someone we get a new manager we change the location we can update it in with this one table and we know that everyone working in marketing is going to have that information which is better than duplicating it in the previous table now where this gets tricky is how do I link and by how do I tell people that well everyone working in programming is also going to have this location in this manager without doing you know without making it too complicated that’s kind of the goal that we’re looking at you can see how both of these programmers obviously are going to share the same information that we’ve got down here they’re going to work in the programming department with that location and they’re going to have Susan as a manager okay so that’s kind of what we’re looking at you can see that obviously marketing is going to associate there and then production is going to associate there and stuff like that but we need to find a better way to connect these together

and that’s why relational databases exist it allows us to pull information out create all these different tables but we can link them together using using these relationships and that’s what we’re going to be doing today so so what we want to do in this case is um we want to restructure our database our users table to adapt to these relationships ok this relationship database model and this is how it works in practice this is kind of this is the way that it would normally work what we would normally do here is we would rename something up here to department ID you remember when we talked about IDs and you know originally we talked about how every item in a database has a unique identifier at the beginning we call as the primary ID okay so it makes sense that the and we’ve also talked about that IDs are the fastest way to find tables and they’re always unique so even if we delete an entry in a database that primary ID never gets reused again so primary IDs are ideal ways to reference other items in other tables because we can just say hey there’s the department’s table and if I could get access to ID number one in the department’s table we we know what that would represent even if some of these other items change that primary ID will always be the same so this allows us to basically create a column up here instead of having a department column we would call it department ID this is just considered good practice you could technically call it anything but if to make life easy and to follow the you know principles and consistencies that almost developers follow you would generally call it the name of the table underscore ID in this case we would have departments so you would do the singular version Department and then underscore ID okay now if you have we’re referencing a user in here you could in the departments table you would do something like user ID to reference back to the users table so in this case we’re going to call department ID but instead of writing these out like this what we’ll do is we’ll just reference the ID number so in this case John Doe works for programming so we’ll just reference this the ID for the second one which you see down here so it references the programming and we can go through and do this for the rest of our table so we’ve got you know we’ll connect marketing which is number one and then production which is number three and then through label we’re going to create these connections telling label basically how this works that each of these connect to these IDs down here okay hopefully that makes sense hopefully that’s the you know an easy way to kind of see this visually and this helps a lot as we start changing information now okay so now let’s say we decide to change the name of the department from programming to say development well now we can just do that by just changing it down here to development in the name because everything is still linked John Doe and Jane Smith are linked to the department number two they’re automatically going to change to development so now when we reference John Doe and you pull his user record out we’re going to look for user ID to which and then when we get the name of his department it’s going to automatically be development we’re not gonna have to change any code in order to make this change because we’re going to create this dynamically these dynamic relationships allow us to basically have this information updated dynamically without having to worry about making sure that the database is up-to-date and any programming changes or anything like that I’m referencing Department number two in the code and now because his his department has changed from programming to nam delle development every time i reference that name column in his that you know that connects to his department ID because i’ve got that relationship I’ll automatically now start getting development for all these users that were connected to that hopefully that made sense to you guys okay databases can get confusing when you start really getting these twisted but anyway that’s basically the way this works let’s go ahead let’s dive back into so anyway let’s go ahead let’s dive back into the application and get started on actually writing one of these relationships ourselves okay so the first thing I want to do to create this type of relationship is we want to start creating we need to create a table to store all of our categories make sense guys you guys should be a well aware of this we’re going to create a categories table in our database that way we don’t need to manually write in our like our post table we don’t need to manually have a categories column where we manually write the name of the category associated with that what we’re going to do is we’re soon going to create a category ID column in the post table and then we’re going to link that ID column to a row in our categories table this will allow us to have tons of

information about categories if we wanted and well it’d allow us to kind of create and update and you know delete called things all sorts of stuff like that but we’re going to do it dynamically with these relationships okay so we need to create a migration and we also need to create a model alright so let’s go ahead and do that we can do that through the terminal I went ahead and made the font a little bit bigger for you guys so debate a lot easier to follow along now that was a complaint I heard from a few of you guys was the terminal text was really really small which I totally agree so first things first what we want to do is we’re going to do PHP artisan make and we’re going to tell it to make a model we’re going to make a category model to work with okay that’s going to be the model that handles us all all the actions that we need for working with categories okay and then soon what we’re going to do is we’ll go back in a minute and we’ll actually connect that model to our post model which we already have okay so let’s create a model we’re going to say make model and we’re going to call it category now you guys should remember from before when we make models we actually just use the singular form and we add a capital letter at the beginning okay so this is the convention that you would normally follow and you wouldn’t call it categories you’ll call it category all right the database table will be called categories plural because it stores many categories but the model will always be the singular form with a capital letter at the beginning all right so this is just going to be category now if you wanted you could also add migration here and I’ll automatically create the migration but I’m going to just do it separately because I’m going to show you guys a trick about migrations as well but either one works however you guys want to do it so we’ll do PHP artists and make model category let’s go ahead and click that we’ve got our model created and then while we’re at it let’s go ahead and create the migration so we’ll do the same thing we’ll do PHP artisan make and this time we’ll do migration we’re going to name the migration create um categories table all right because that’s we’re doing we’re creating a table for our categories and the other thing that the little trick I wanted to show you guys is you can also do something here where you can add a tag to this line where we can say create equals and then the name of the table that we want to create categories like that and let me show you what that does in the in the code so let’s go and click enter it creates a migration you can see it’s called create categories table let’s come back over to our code and then take a look at what we just created so we’re going to come over here to our our database and then our migrations and it always going to be at the bottom because he’s a timestamp so he coups we click the time stamped here and you can see this says create categories table that came from the name that we created up here and that this is a migration what you may see that’s different from the last time we create the migration was that because we added this dash dash and then create equals categories it automatically knew to create a categories table so we can say here you can see it automatically created the schema create and then categories giving us the name of the table it automatically created a tie a primary IDs and on map you created time stamps just a little little fun thing to learn it also automatically added the drop and normally you wouldn’t get this you would just get an empty file when you do it without that create so without this little tag up here so it’s just a fun way helpful a little way to create migrations that you guys might not know about alright so and remember that dash dash create is if you’re doing a brand new table I’m going to show you’re going to use dash dash table if you’re using an existing table which we’ll do in a second here when we do our next migration okay so this we’re working on our categories table the categories table only really needs one piece of information right now we’re just gonna have the name of our category okay I can’t really think of anything else to store in our categories table and this may not be a great example of of a table because normally with relational databases the value comes because you have lots and lots of data in the table but in this example we’re just going to have one column in addition to these columns that already here we’re going to have a column for the name of the category and that’s it so we’re going to go here and on the second line we’ll just do table and we’ll do string and we’ll say name ok that’ll be the name of the category and we don’t need to do anything to the down function because it’s a drop which is what we’re just going to drop the database if we have to rollback that’s all I need to do for this migration this is going to create a table called categories with a primary ID and a column for the name and then the timestamps so I’m going to go ahead and save this we’ve got that saved I’m going to close that and then the next thing I do before I connect my tables is I’m going to go through and edit now the posts table we’re going to go to the post table and create a column in the table that has our that’s going to link to the category for each post we’re

going to do this with category ID so if you remember in our example we did Department ID this time we’re going to do category ID and then that column is just going to have an integer in it that links to the primary ID in our in our categories table okay so let’s go ahead and do that real quick we’re going to make another migration now or do PHP artisan migrate make I mean sorry make migration and then we’re going to do add or you can call this whatever you want but you just be descriptive when you do it we’re going to say add pote or category ID to posts alright and then we’re going to do the opposite like what we did up here we automatically assigned a new table to be created called categories in this case we already have that we’re going to be adding something to an existing table we’re adding it to the posts table so instead of doing create we’re going to do table so we’ll do table equals posts alright and now it’s automatically going to link our to the existing table so we can just add a column to the existing table instead of creating a new table okay so let’s go and click enter it’s going to create that migration if you come back over to sublime here and open up you can see now this is the one we were just looking at there’s a new one here and in this one we can do this is our add category ID to posts and you can see that it automatically is linking to the table called posts and we just need to add whatever we want to add here and that’s it so let’s go ahead and do that we’re going to do table and we’re going to do integer integer like that and we’re going to call this column category ID just like that okay so that’s the column that we have it’s going to create a column an integer column because that’s all needs to be as a number an integer column called category ID next thing we’re going to do is we’re going to also add a few extra things here at the end I’m going to make it optional only reason I’m making an optional is just in case you forget to add a category it’s not going to break saving also because this is going to prevent us from having to write a script to go back if we make it required we have to go back and make sure that we don’t have a category we have a category associated with every single post for this example I’m not going to worry about it in production you may want to worry about it it might be something you wanted to worry about for now I’m going to do NOLA bull which basically means make it optional alright the other thing I’m going to do here is I’m going to do after slug that’s just going to place the column after slug and before that created at and updated at in our database not needed just something I want to do and then the last thing I’m going to do which is going to be kind of important to do is I’m going to add unsigned like this okay so we’ll just do unsigned and what unsigned means is that with an integer you can have with integer columns in level or any database you can have signed and unsigned integers signed integers are going to have a plus or a minus which means they go up to like I think it’s 256 and down the negative 256 in this case we don’t need to ever go negative all of our primary IDs are always going to be positive so we can put make it an unsigned integer meaning instead of storing all the positive numbers and the negative numbers just store the positive numbers never worry about a negative this allows with the exact same amount of memory for us to go up much higher it doubles the amount of memory or it cuts the amount of memory in half basically and allows us to go up twice as high with our integers okay just get in the habit when you’re connecting foreign IDs that you want to that you want to do this okay so that’s basically all we need to do I am going to address one thing here just to prevent potential confusion and comments because I know this is going to come up in the comments I’m going to hit I’m just going to go ahead and hit this head-on there is also on a link to this in the description there is also another helper when you’re creating migrations called table foreign now I haven’t really used this verbage yet in any Akash I wish I could type I get talked and I can’t type and talk at the same time anyway there’s one called foreign table foreign and this basically is where you manually assign in the database database level you actually tell the database that you’re creating a foreign key okay so what you would do in this case is you would do foreign category ID and then you would say references references ID on categories all right so this is basically what you would do this is something that you can do when you’re working with it I know a lot of people are going to probably mention this in the comments that’s why I wanted to bring it up head-on what I honestly never do this you can do this but the thing to keep in mind is that not every database supports supports this type of

encoding if this makes sense by default it’s by default my sequel installations are generally in ODB i NN ODB it’s sistah just an encoding for the database and I own the in ODB doesn’t support foreign key manually you know written into the database it doesn’t actually support this feature so this is I don’t actually so I don’t think this throws an error but it just doesn’t do anything is what it does when you have Ino dB if I open up my database I’ll kind of show you guys when I come over here to blog you can see that this you can see that the database engine for this table is in ODB alright now when you’re creating a new table there are other options here for all sorts of different types of tables what the other popular date database encoding is called myght this my isome and this one does support foreign these foreign key relationships directly in the database but it’s also slower at retrieving data and there’s trade-offs and this is going to want to start a whole discussion on which one’s better because this is a long debated discussion on which one’s better the in ODB or my Isum i don’t want to get into that some are faster has certain things some are slower other things and vice-versa it goes around there’s there’s definitely benefits to the two different kinds one of the differences is that my Isum supports these foreign key references directly in the database if this makes sense we’re not going to be using those though what we’re going to be using is we’re actually using some references that happen on the level level level is actually going to create because of our reference it’s actually going to manually create two queries when we reference something via a primary or a foreign ID I hope this isn’t going over your heads guys but this that’s basically what’s happening behind the scenes because of the restrictions that nodb has which is the default my sequel type alright so the long story of all this is basically to say that you’re going to see these foreign key things in some of the documentation if you go to layer Bolcom you probably don’t need to worry about it I honestly never worry about it we’re going to set the relationship a different way through actually like actually hard-coded in label and then it’s going to use PHP to manually connect those instead of relying on the database to do it itself if that makes sense the reason I don’t rely on this is just because I generally am not using my Isum so it generally isn’t supported at all so this is extra code that doesn’t do anything and I don’t believe it’ll throw an error it might but I don’t believe it’ll throw an error I think it just nothing happens so it just it just as extra complicated on something that doesn’t need to be ok I just want to I just want to hit that in the bud before it gets brought up in the comments I just want to you know bring it up so everyone can kind of see you know understand what’s going on with that okay so that’s why I do it this way however the one thing you need to make sure that you do have when you are creating a foreign key type thing like this is you want to it needs to be an integer and you’re going to want it to be unsigned all right make sure you have at least those if you want to get rid of these you can I like to keep it NOLA bolused because um just so I’m not forcing the category on someone and then after is own is not important at all unless for me you’re actually just using a GUI to look at the database or something I don’t like how I like having created at and updated at the very end so I want to pull this up right before the end so that’s kind of why I’m doing what I’m doing so that’s where I all of this just in case you guys are wondering all right now moving on we should have this is going to create a category ID column in our database our posts tape table and then down here what we need to do is we just need to do the drop a column so we’re going to do table drop column and then we’re just going to drop the category ID column and that’s all we need to do for that and that’s just in case we roll back our database okay the next thing we need to do is actually run the migration let’s go do that we’ll do PHP artisan migrate that’s going to run our two migrations we have two of them pending that we hadn’t ran yet and if we go back to our database where as where I was before and we look at our blog we will see now that we have a categories table with an ID a name and then our time stamps and now in our post table we’ve got this category ID right here okay let’s head back over to sublime and now we’re going to do you can actually clean up everything we’re working on we don’t need the database anymore or our configs next thing we’re going to do is actually go to our model so come to our app and then we’re going to go to category and look at the new model we created you can see there’s nothing in here yet but if you remember from our previous stuff remember that everything got inherited from this extends model so we have all the basic

crud functionality built in the ability to find and replace and all that stuff’s built-in so even though it looks empty it’s actually packed full of useful stuff all right in this code block here is where we can overwrite what got extended in the model and we’re going to do that right now the first thing we’re going to do is we’re going to manually tell this model to use the categories table normally this is not needed but I just like to when I I just like to do this when I have funky cat table names okay or if you’re using a table name that’s outside of the convention then you need to manually tell the model what table to use okay if you have a user model it automatically assumes the users table so you don’t need to manually write this if you have a post model it assumes the posts table now if you get weird with your conventions and you don’t follow the conventions and you call let’s say you call a you have a post mom but you call the the table blogposts then you need to manually tell it because it’s not it’s going to be looking for posts and you need to manually tell it it’s called blog posts in this case I’m going to manually tell it that it’s called categories okay because it’s a little bit different spelling I don’t think you actually need to do this but I just want to show you guys anyway so we’re going to create a protected attribute we’re going to call it protected we’re going to say table equals and then we’re going to set it equal to categories like that okay that’s all you need to do now you’re telling laravel use the categories table when working with this model again we didn’t do this before because we were following the convention we had we create a post model in new that it goes to the posts table we created a user’s model in new it assumed that it goes to the users table as well okay so this is why we’re defining it this time and we didn’t last time okay moving on the next thing we need to do is actually define the relationship so this is where we actually are going to create a method and we’re going to tell it that the category is going to have many posts okay so one category item if we create a category called PHP tutorials that category may have a whole bunch of different posts in it okay we call that it has many relationship okay or one-to-many relationship this is the category is the one and it has many posts okay and that’s what we’re going to be telling it here now to set up that relationship we create a public function we name the function whatever we want but it’s really good to follow a convention in this case we’re going to call it posts okay because it belongs to many posts so we’re going to call it posts and inside of here all we need to do is say return and we’re going to say this has many and then and then we’re going to tell it the model that it should connect to so we’re going to say app hat and then make sure you in the backslash again not not this type of slash but that type of slash we’re going to app post and we’re going to say that it has many posts as basically or saying here okay so we’re connecting it to the post model okay that’s all we actually have to do there and it’s going to connect it now to the post model and it knows that this ID is going to associate with a categories ID in the posts model all right that’s all happening behind the scenes in level and level is assuming that all right I’m going to link this in the description if you want to read up more about these relationships and you want to see kind of more of what’s happening what we’re doing here is what’s called has it’s called one-to-many relationship so take a look at the one-to-many part we’re going to be working on many to many in the tags video okay we’ll go so we’ll go over both of those here the one-to-one relationships are identical to the one-to-many relationships which is what we’re doing here so if you do have a one-to-one relationship that you want to work on it’s almost identical the only difference is instead of has many here you will do has one and that’s all you need to do alright that’s the only difference otherwise you set them up exactly the same obviously the difference mean is saying that the category would only belong to one post in this case has many says it can have many posts that’s the difference all right but I’ll link this in the description if you want to learn more about how to do this there’s some other options you can add at the end of has many if you need to again if you don’t follow the conventions and you need to manually define the you know the column name that the ID that the integer is in then you would set that up in here okay but we’re following the convention we created a table column called categories ID or category ID and so we don’t need to do that because we followed that Convention so label already knows what to assume okay so we’ve created one part of the relationship we said hey the the categories have many posts well now what we need to do is we need to go to our

post model and say that set up the other half of the relationship saying that the posts are going to belong to a category all right so let’s go over to the post model and set that one up so I’m going to go and save this close our category model and set open up our post model and here we’re going to do the same thing so we’re going to create a public function and we’re going to call this one categories all right and inside of here we’re going to do the same thing we did before we’re going to say return this but this time instead of has many we’re going to say belongs to like that all right so we’re saying that this is the post now the post belongs to a category all right and the categories have many posts hopefully that makes sense again links in the description if you want to read the label documentation on this now inside of the parentheses here four belongs to we’re going to actually connect it to the model for categories all right in the categories model we connected it to the post model in the post model we connect you to the categories model so they’re both pointing at each other that’s the important thing keep that in mind guys okay so we’re going to do belongs to app back slash category okay just following the exact same convention again if you want to look at the label documentation you can see how to connect different types of foreign keys or if you want to customize that or something like that okay that’s all we need to do for this we now have created something where they connect and talk to each other so now let’s go ahead and test it what I’m going to do it for this tutorial just to finish up this tutorial is we’re going to manually create a category in our database and then we’re going to manually link it in the database but in the next video we’re actually going to create the user interfaces where we would make this happen with that the users would connect categories but just to show that everything we’ve done works in this video I’m going to go ahead and do it manually and just show you guys how it works and it’s going to be good to visualize how the database is being created okay so let’s come out over to our I’m opening up our database here you can see here that we’ve got empty category IDs if we go to categories we have no categories so let’s go ahead and create a category I’m going to click it and I’m just going to call the category PHP tutorials alright just like that and we don’t we’re not going to create the created add or updated app but it would normally be created when you do it so we’re not going to worry about it right now okay so we created one category called PHP tutorials let’s create another one actually and we’re going to call this one Ruby on Rails tutorials I am going to be doing some Ruby on Rails tutorials coming up as well I love Ruby on Rails I use Ruby on Rails a little bit more than he’s label for clients just because Ruby on Rails to me is a little bit faster and easier to use but I actually have more experience with level ironically Ruby on Rails is a little bit newer for me but it’s actually a really cool it’s a great framework I actually really like using it it’s got a lot of cool stuff in it but anyway we’ll be doing some of those as well I want to go back and recreate this application that we just did in Ruby on Rails so you guys can learn it both ways and decide which framework is right for you okay there’s pros and cons to both of them that’s why I know both of them is because there are times when PHP is going to be better to use and there’s times in Ruby on Rails is better and there are pros and cons and I’ll talk about that in a future video okay anyway we’re going to create two categories ones Ruby on Rails tutorials ones PHP tutorials okay you can see that each have an ID number one and two and now I’m going to connect posts to our categories so let’s go over to our posts table and in this category ID what I’m going to do is I’m going to give it a number either one or two okay so we’ll just do one two two and one alright so we now they’re all linked to a category all right now let’s go let’s go on let’s create just a part in our view so that we can see how these work and that will finish up the tutorial but I want to see I want to show you guys that these are now connected together all this code we did all this database stuff and they’re now connected together so let’s head on over to ours to sublime and what I’m going to do now is I’m going to open up our we’re going to open up a view and then let’s go to our single blog post okay so under views we’re going to go to blog post and single this is this basically shows an individual blog post and it shows the title and then the body all right this is weird like a public the public would read our blog post what we’re going to do is create a horizontal rule and then we’re going to create another paragraph down here and we’re going to say posted in this category okay so post it in and then we’ll do our double brackets and this is where we’re going to reference post now

just – we’ve jumped right into this view I just want to mention that we can access post because you can see we’ve got the whole post object here and this is why relationships are cool even though we had that like ID number over there we can actually access the information in the other table because the the two tables are now connected with that ID number so when we grab an object just like we did before look I’m not changing anything in the controller the controller is exactly the same the controller went out and found the post with that slug and it just grabbed all the information about that post and then threw it into the view so now the exact same object I had before I can now access the name of the category with the exact same object all right I didn’t need to change anything in the controller add any other code so now what I’m going to do is I’m going to come to post I’m going to call it category and then name and I’ll show you in a second why I’m doing that but this is what I’m going to do I’m going to do post category name and what I’m expecting is that it will show the name of the category that this blog post belongs in I’m going to go up here and save this and we’re going to boot up the server and then take a look at it okay so we haven’t booted up the server yet so I’m going to go over here and PHP artisan serve and and then here we are right here so we’ve got our blog post and I’m just going to click on this first blog post here we’re going to read more and you can see that this is posted in PHP tutorials and let’s come let’s just head back and let’s click on the next one read more and this one’s posted in Ruby on Rails tutorials so it’s pulling the name of our category directly out of that database it’s automatic connected thanks to those relationships we set in the models all right so this is pretty cool guys I hope you guys see how cool this is this is pretty sweet okay so this is really fun stuff I just want to show you guys real quick we head back over our database and in fact what I’ll do here I’ll just great balls of fire is this one right here I’m just going to change this category right now to one this is going to connect it from Ruby on Rails now it’s going to say PHP tutorials and click enter and come back over here now and just refresh and you can see how it automatically pulls that in just as super easy right so these are these two tables are connected I hope you guys see how cool that is and how powerful that is we’re doing a very simple use case but you can start adding tons of information through relational databases now because of this all right so I just want to show you guys that now the last thing I’ll show you if I come over to categories and I just let’s call this PHP tutorials we’ll just call it PHP Tut’s like that and now if I come over here and refresh you can see how it’s automatically updated as well all the databases are connected together they’re automatically pulling information and stuff like that alright last thing I’ll show you before we finish up the video is let’s come back over to our project and you’re probably wondering why I was able to I didn’t chose category and the name all right so I just want to show you that real fast I’m going to open up our model for post ok since we’re using we know this post object is a object of the post model alright which is this okay so that dollar sign post is an object of this model right here all right so what we can do the reason I did that I called it category the category method was because of the category property was because we have this category method right here okay this category method initiates this relationship now it also as part of what it does is part of what this belongs to does it sets up not only the method but also the property and that’s what I was accessing here you can see I didn’t use parentheses we did not do this if we did this little break watch if we do the parentheses and I refresh we’ll get an error now okay and that’s because what we’re actually doing here is we’ve actually we’ve actually like initiated method okay we don’t want to do that just Larry will handle the method what you’re at what you’re going to be calling is the property okay which doesn’t need the doesn’t need the parentheses okay and that’s automatically created here so we create the method called category with the dollar sign so okay so that’s the category so it’s because of this so we just access this property and then once we’re inside of here we have access to all of the column names inside the database so inside of our database we have a column for ID number for name for created at and for updated at so if I set this created at real quick I’ll do it too okay so I’m just setting some

information here for our database not doing anything fancy I just want to basically I just want to basically show you guys that we can access these other properties right so that’ll be fine all right so I added a created and updated X label and so now if we come back over here I can access anything in the category database all right now I just chose name but if I wanted to do create it at I could grab the created at value in here as well so let me come back here now you can see that we’re grabbing the created at item I could also call the updated ad and you can see it’s going to change a little bit or the ID number or anything like that I can I can choose any item from there because we’re actually grabbing all of the this is the actual category ID all right so we can grab all of that information from here and you can also just grab the category ID without initiating the relationship so this would just grab the category ID out of the post column which would also be one okay so anyway just want to show you guys how that works this is pretty cool because as you start getting to like pulling in entire user records you can actually do something like this if you had like an author you could grab the users birthdate you know I mean because you would probably have the birth date in the users table and so you could now connect the posts author like so you could have maybe something like this the post author would probably have a would probably link back to the users table and have all of it and then you could get anything out of the users table for the person that wrote the blog post if that makes sense that’s why this is so powerful okay let’s just undo everything and we’re going to go back we’ve got category and name and that will give us the name of the category the name of the category for this tutorial all right so or for this blog post I mean so hope that hopefully that makes sense guys that is a ton of information about relational databases explained it a lot in the next video we’re going to dive through the crud that’ll go pretty quick because we’re going to be doing rashmi creating the crud for our categories so we can create categories and link them to posts and stuff on the front end of the website that’s what the next video is all about so be sure to join me get if you join me for that one guys that’ll be a lot of fun but I hope you saw a lot of value in what we learned today this is a powerful concept and once you master relationships it really expands what you’re able to do as a side know a lot of the questions I’ve been getting people emailing me and stuff are asking me like okay I want to create this type of app a dating app a social media app whatever and they asked how would I set my database up and it almost always comes down to relationships you have to have lots of complex relationships to make it work if you’ve got a social need social network you’ve got to have users but then if they’re going to say like a photo those photos need to have relationships with if you know their photos the belong to their profile need to have relationships to their profile and then if they like those you have to have all those likes have to have relationships to the person that liked them and all the comments have to have relationships to the user that wrote them and also relationships to the posts that they belong to it gets very complicated with all the relationships that you need but relationships are pivotal in learning to program more advanced applications from just the basic stuff we’ve been doing up at this point all right so you really want to spend some time to understand and you know better understand relationships in data cases hopefully you learned a lot from this one but of course this is only the top this is only skimming the top of the information that you will want to learn as you start growing as a web developer okay so this is only just Tate you know getting your taste buds you know trying this stuff out but there’s so much more all right the next video we’re going to work on user interfaces for our categories I’ll see you guys