![]() Discover how to animate your React app with AnimXYZ.Switch between multiple versions of Node.Use React's useEffect to optimize your application's performance.Learn how LogRocket's Galileo cuts through the noise to proactively resolve issues in your app.Don't miss a moment with The Replay, a curated newsletter from LogRocket.Similar to how relationships in relational databases work, there are other types of polymorphic relationships. The example above has covered one-to-many polymorphic relationships where one model, e.g., a page, can have many models, e.g., comments, related to it. Let’s create our migrations: Schema::create('posts', function (Blueprint $table) Types of polymorphic relationships in Laravel Here, we have three entities: Post, Page, and Comments.Īnd Comments can belong to either Post or Page. The commentable_type would store something like App\Post, which is how the ORM will determine which model it belongs to and return when trying to access the value. The commentable_id column would contain the ID of the post or page, and commentable_type would contain the class name of the model that owns the record. In the example above, we have merged page_comments and post_comments together by replacing both post_id and page_id in each table with commentable_id and commentable_type to derive the comments table. We have two important new columns to take note of: commentable_id and commentable_type. posts:īy definition, polymorphism is the condition of occurring in several different forms, and this is the approach we are trying to follow above. ![]() ![]() With polymorphic relationships, we can follow a cleaner and simpler approach for the same situation. This approach above has us creating multiple comment tables - posts_comments and pages_comments - that do the same thing except that they are pointing to different entities. To allow for comments on each of these, we can decide to set up our database like this: posts: Taking the example mentioned above into consideration, we have two entities: Post and Page. What are polymorphic relationships in Laravel? In this article, we will go over polymorphic relationships in Laravel, how they work and the various use cases in which they are best used. This kind of behavior is described as polymorphism. In a blog, for example, comments can go on a post or a page but maintain the same structure regardless if it’s a post or a page. This type of model maintains the same structure that doesn’t change regardless of the other model it’s connected to.Ī common example of this kind of scenario is comments. It’s not uncommon in software development to have models that can belong to more than one entity. Polymorphic relationships in Laravel and their use casesĮditor’s Note: This post was updated in September 2021 for accuracy and to include information on multiple types of polymorphic relationships in Laravel, including one-to-one, one-of-many, and many-to-many relationships. Building communities and open source for the Next Billion Users. Adewale Abati Follow Web engineer, tech lifestyle YouTuber, public speaker.
0 Comments
Leave a Reply. |