Laravel 6 Advanced - e3 - Polymorphic Relationships

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome to another episode of level 6 advanced in this episode we're going to be covering the polymorphic relationships now there are three polymorphic relationships in level and if you're not familiar with the term polymorphic it is a base core value of object-oriented programming and it's just a fancy word that basically just means that an object can take various shapes so the example that we're going to be starting with is the example that there are a posts and there are users and they can each have an image so imagine a post it would be an image that represents that post and then we're gonna have a user maybe profile image now if we don't do this the polymorphic way you're going to end up with two image tables one for the posts and one for the users but the nice thing with using polymorphic relationships is that we can use the same image table for both of these or as many other relationships as we want so that's the nice advantage with polymorphic it's a lot easier to see than it is to explain so let's jump right to it like I said we're going to have this idea that we have posts and users so let's start with posts let's create a model make model for post and I do need a factory as well as a migration now our users table that's already created for us that comes out of the box so the other thing I need of course is going to be an image so let's go ahead and run the same command but this one will be for image and I actually don't need a factory for that so we just need a migration so back into phpstorm this is a totally fresh level 6 project the only thing I did was just set up the database behind the scenes so let's jump right into the create post table and we're going to keep it simple or just simply say table you're going to have a string for a title as well as a text for a body so now let's to the post factory that way we can create some on-the-fly so we're gonna have a title we'll just use faker for that so we'll save faker give me a sentence will use a paragraph for the body alright pretty simple setup there with the post all right so now with our users like I said those are ready to go so let's take a look at the create images table remember this is going to be the one thing that we want to share with users and posts so the way that it works is that you basically need to have two special columns inside your images table in order for polymorphism to work the first one is going to hold the ID of the item that it is associated to so in this case say that it's for the post then it's going to be post ID now typically you'll have that as something like unsigned big integer and you'll say post ID but the problem with this is that when it's time for the user and it's not going to work the post ID doesn't exist on the users table so we need to find sort of a term that can apply to just about everything and the way that level handles it is just by adding the able at the end of it it's a suffix for the word so in this case we'll say image a bowl so this is imageable and like I said this needs to be an ID so this is going to be the imageable ID and the second special column that you need is going to be a string for the actual model that this is related to we'll use the same word again it's gonna be imageable underscore type this will become clear in just a second when we actually take a look at one example already built up so again two special columns one holds the ID of the related model in the case of the post this will be a post ID in the case of the user this would be the user ID and then we need a string to actually let it know what this ID actually belongs to so this is going to be the model the type of relationship that we have so in order for us to have an actual image let's just simply have maybe just a URL string again just to keep this as realistic as possible but still very simple so this is going to be our images table let's go ahead and migrate our database so I'll say PHP artisan migrate so it migrates everything and in sequel Pro I do have this pulled up so there it is so we have our images table which just simply has a URL and imageable ID and an image of all type and then we have our post table let's go ahead and whip up one post through tinker so PHP artisan tinker let's create a new post and let's use the factory for that of course so say app post class creates perfect so now we have one post so now we want to associate this new image so let's go ahead and set up this relationship so let's jump into the post model and a post like I said it can have one image it's going to be the one image that actually represents this post so let's add a relationship and it's going to be called image and then in here this is where the magic happens the relationship that we're going to use here is the morph one relationship and then what are we morphing what we're morphing and image so we can just use image class and then we do need to let it know what is the term that we are using and like I said it's gonna be imageable and now to set up the inverse of that relationship let's go into my image and then let's go ahead and first and foremost go ahead and turn guarded off this is due to mass assignment we've talked about this before but then the inverse of the relationship is going to be imageable it's that same word that we used because remember we cannot put post or user because it's gonna change so again we're gonna use that term that's really vague and doesn't really speak as to what it actually is it's just imageable that's all it is and so the relationship here we're gonna return this morph too we're actually gonna use morph - quite a few times so that's it the relationships are set let me turn off mass assignment out of the post as well say guarded equals an empty array so let's go back into Tinker and I need to actually exit out let me put it back up and now let's grab that post that we created so our posts will be under post first I'll be the one post that we have so now we could say post image we can use that relationship to create a new image and to this array of course all we need is just the URL of some image that of course doesn't exist so we say some image the jpg and there we go so let's check out sequel Pro so we can analyze what actually happened let's take a look at our images table so check this out so we have the URL this is our some image JPEG and then we have imageable ID the ID of one belongs to the post table how do we know that well because we have this imageable type so the imageable type is telling us that this ID belongs to this post table let's keep going and I think it's gonna make a little bit more sense once we bring in the users so to bring in our users it's actually quite simple we're going to go ahead and pull open my user and let's just add the same exact relationship that I have in my posts I'll go ahead and copy it and at the very end of this I'll just add that in and now a user can have an image so let's actually create a user again using app user creates and I will need to exit this because of course I changed the code so now let's go ahead and fetch that first user will say user first and on that user we can add a new image and again using that relationship we can say alright create a new image so we'll say URL just gonna be profile dot jpg again this doesn't exist I'm just making it up as I go so back to sequel Pro now this images table has an idea of one for user and an idea of one for post and so that's how it works so this image cannot belong to more than one user and the same with this one and at the same time a post cannot have more than one image and a user cannot have more than one image because if it does then it's not a one-to-one relationship it would actually be a one-to-many or a many-to-many so let's move on and actually take a look at a one-to-many relationship so to do that let's bring in a whole new concept to our application and it is the concept of comments so of course you can leave some comments inside your posts that makes total sense but a post can have many comments but a comments always belongs to a single post so that is a one-to-many relationship so to bring that concept into our application we're going to need a new model we're gonna need a comments model so let's say PHP artisan make a model for eight comments and I do need a migration so we'll pass the - M flag okay so let's jump back to phpstorm and see how that one would work I'll close everything up and let's bring up comment that PHP the first thing I'll do is I'll turn guard it off of course we definitely want to do that so again a comment belongs to a post but a post can have many comments so let's set up the comments table to accept that so as we did before we're going to need an unsigned big integer for commentable underscore ID and we're going to need a string for commentable type so again this word is just comment and then we add the able at the end that's just a convention that it follows so let's take a look at the post model and let's add this new relationship so public function of course is going to be comments so for comments what is the relationship that we have well when we have one too many polymorphic relationship the post side of it will use morph many and then what are we morphing we're morphing of course the comment class or using the commentable keyword so with that setup let's go ahead and setup the inverse of this relationship and so of course let's go ahead and add a new public function here and it's going to be called commentable and this is simply returns this morph to same as the one-to-one relationship okay so so far so good with this let's go ahead and migrate the database and now of course we're going to have a new table here for comments and with this I realize I didn't add a field from like actual comment for the comment body so let's fix that now very quickly back to my comments table let's add a new field for text body all right so let's roll back the database and then REME I great that one more time and now there we go so now we have a body and of course the body here is going to be the actual comment that the person left on our post so let's go ahead back into PHP artisan tinker let's fetch a post so we'll grab the very first one again and now let's create a comment so say post comments and again we'll use that relationship to create a new comment the only thing that we need in here is a body and we'll just say first cool comment so now let's check out what we have so again we have a comment here for a post with ID of one and it is first cool comment so not a lot of difference here from when we did the image however we can actually add another one so let's run the same one again and I'll say again on this one now if we come back here notice that the one and the app post remains the same but now we have two comments so we can have as many comments as we want into our post and in the same token of course why don't we create some comments for something else what about some videos so let's introduce the idea of videos into our project so we'll say a PHP artisan make a model for a video and I do need a migration for that and I actually won't even generate anything I'll just simply run PHP artisan migrate and now we have this idea of a video so let's just manually create a video and all I'll do here is just put some timestamps that way we have a record I just need a single record alright so back into tinker PHP artisan tinker let's grab this video by calling video first and with this video I can call comments but of course I actually have not created that relationship yet so let me add that in let me go into video and in my video model all it needs is the same thing that my post has this exact same code right here so as soon as I add that in now this video is commentable let me go ahead and turn mass assignment off again on this one alright so now we're actually ready to run this PHP artisan ticker and so we'll grab our video and now from our video we can use the comments relationship to create a new comment alright so we'll say the body is going to be video comment alright so that is created back into my comments table now you see that we have a video comment and it has the same idea of one but this one is from the videos table and again we can have this in as many tables as we so it's as easy as that to add a comment and that brings us to the last polymorphic relationship and that is the many-to-many relationship now to demonstrate this one we're going to need two tables and the concept behind a many-to-many that we're going to use is the concept of tags so when you tag something you're going to have many tags now a tag doesn't belong to a single entity but a tag belongs to many entities and at the same token that entity can have many tags imagine a post a post can have many many tags for example could be level could be MySQL and may be eloquent and at the same time the eloquent tag for example in our application could have many posts so that is a many-to-many relationship so let's bring in that concept into our application let's go ahead and create a new model for tag now we'll need a migration for that one and like I said I actually need a second table so let's create a migration to create the glue it's going to be a separate table that glues together the two tables that we are applying this many-to-many polymorphic relationship to so let's create a new migration with PHP artisan make migration we're going to call it creates taggable table we'll pass in the create flag for taggable and to keep it consistent this should be taggable alright so that's created for us so let's start there let's go back to phpstorm and let's go to the create taggable stable now this is a big increment and it is ID however we actually don't need an incremental ID so I will remove that and let's use unsigned big integer for tag ID we do need a tag ID remember this is going to be very similar to a many-to-many regular relationship except this one is many to many polymorphic so we're going to need a tag ID and again we're going to need the taggable ID as well as a taggable type and is going to be a string and that's it that's all we need on this one now on the tags table all we're really going to need is just the actual tag so let's add a string or maybe name okay so now let's create this idea in relationships let me go to my post and let's start with that one again let's add a third relationship here four tags so a post can have many tags so the relationship that we're going to use on this one is going to be morph to many and then we'll use the tag class and again the magic word that we are using is taggable and then over in my tag model let's go ahead and disable mass assignment again so say guarded equals empty array the relationship set up here is a little bit different we do need to associate each of them to what they actually are so the first one is going to be post so a tag can have many posts and so the relationships that we use here is morphed by many morphed by many what wealth morphed by many posts that's the model that we're going to use and the magic word that we're using is taggable okay easy enough so now let's go ahead and make some tags so back into tinker let's grab the first post one more time and from my post I can use that new tags relationship to create a new tag so for my tag all I need is just the name and so this first post is going to belong to maybe a level tag whoops forgot to migrate the database I'm sure you saw that so PHP artisan migrate there we go all right so now let's try that one more time I'll pull open the same commands and creates and sure enough we created that so now we have two new tables here we have our taggable table which we already see has an entry for tag ID of one with taggable ID of one remember the taggable ID belongs to the post table and then on top of that we're going to have our tags so there is our laravel tack alright let's create another tag what about the idea we said of eloquence okay so now our tag gets added to our tags table and of course we add yet another tag to our taggable table okay what else can we tag what about videos videos are definitely taggable so let's create that relationship first and then we'll test it out in tinker so let's pull open the video and of course in here I'll have a new relationship for tags and the relationship that we use in this case is the same one that we used for the post so it morphs to many I'll copy this one and let's go ahead and bring it into the videos table then in my tag I do need to create a new relationship because again we do need to separate each one so this is gonna be four videos and we're gonna morph by many using the video model instead and then we're still using the taggable on that one all right so let's reboot tinker so exit PHP arson tinker let's grab the first video video first and then let's create a new tag for my video so tags I'm going to create one and I'm going to attach an existing one so I can show you what the difference would be so for name on this one maybe we'll say PHP and now when we go back to our table in our taggable we do have this new app video and for our tags of course we have PHP let's also attach laravel to that same video so to do that we can use the keyword attached so instead of creates we're going to use attach and we need to pass in the ID so the idea of label is one so we're going to attach one and now when we come back into my tag opposed table there you go so now you see that we have tag of 1 and tag of 3 both associated with video with ID of 1 so that's it that is polymorphism in level now I do consider this an advanced relationship and you don't reach for them very often but they are extremely extremely powerful so as always don't forget to subscribe if you have any questions drop them in the comments below and share all the videos that way we can continue to spread the word about coders tape my name is victor and thank you for watching
Info
Channel: Coder's Tape
Views: 60,266
Rating: undefined out of 5
Keywords: laravel 6, laravel best tutorial, laravel 6 what's new, laravel service container, laravel ioc container, service container, service container laravel, container laravel, laravel ioc service container, laravel advanced, laravel view composer, view composer laravel, laravel view data from database, view composer, eloquent, Eloquent polymorphic relation#3, polymorphic relation, polymorphic relationships, polymorphic relationships laravel, polymorphism, polymorphic relationship
Id: C7T1689IvPQ
Channel Id: undefined
Length: 21min 33sec (1293 seconds)
Published: Tue Aug 27 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.