#Laravelmanytomanypolymorphicrelations
Explore tagged Tumblr posts
Text
Laravel Many to Many Polymorphic Relationship
A large number of Polymorphic relationships are also a little difficult to grasp. For example, if you have posts, videos, and tag tables, you'll need to connect them all to meet your needs, such as having many tags on each post and the same for videos. In addition, several tags are associated with multiple posts or videos. However, we can easily accomplish this with just one table, "taggables." You only need to read the article to understand it. In the application of Laravel 6, Laravel 7, Laravel 8, and Laravel 9, there is a many to many polymorphic relationship. In this article, you'll learn to create polymorphic many-to-many relationships using migration with a foreign key schema for one-to-many relationships, sync with a pivot table, create records, attach records, get all records, delete, update, and everything else related to polymorphic many-to-many relationships. I'll make tables for "posts," "videos," "tags," and "taggables" in this example. Each table is linked to the others. Using the Laravel Eloquent Model, we will now construct many to many polymorphic associations with each other. We'll start with database migration, then move on to modelling, retrieving records, and finally creating records. For the relation, polymorphic Many to Many Relationship will employ "morphToMany()" and "morphedByMany()"
1. Create Migrations
Now we must migrate the tables "posts," "videos," "tags," and "taggables." So, let's make something like this: posts table migration Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string("name"); $table->timestamps(); }); videos table migration Schema::create('videos', function (Blueprint $table) { $table->increments('id'); $table->string("name"); $table->timestamps(); }); tags table migration Schema::create('tags', function (Blueprint $table) { $table->increments('id'); $table->string("name"); $table->timestamps(); }); taggables table migration Schema::create('taggables', function (Blueprint $table) { $table->integer("tag_id"); $table->integer("taggable_id"); $table->string("taggable_type"); });
2. Create Models
We'll make a Post, Video, and Tag table model here. For both models' relationships, we'll use "morphToMany()" and "morphedByMany()" Post Model Video Model Tag Model
3. Retrieve Records
$post = Post::find(1); dd($post->tags); $video = Video::find(1); dd($video->tags); $tag = Tag::find(1); dd($tag->posts); $tag = Tag::find(1); dd($tag->videos);
4. Create Records
$post = Post::find(1); $tag = new Tag; $tag->name = "CodeSolutionStuff.com"; $post->tags()->save($tag); $video = Video::find(1); $tag = new Tag; $tag->name = "CodeSolutionStuff.com"; $video->tags()->save($tag); $post = Post::find(1); $tag1 = new Tag; $tag1->name = "CodeSolutionStuff.com"; $tag2 = new Tag; $tag2->name = "CodeSolutionStuff.com 2"; $post->tags()->saveMany(); $video = Video::find(1); $tag1 = new Tag; $tag1->name = "CodeSolutionStuff.com"; $tag2 = new Tag; $tag2->name = "CodeSolutionStuff.com 2"; $video->tags()->saveMany(); $post = Post::find(1); $tag1 = Tag::find(3); $tag2 = Tag::find(4); $post->tags()->attach(); $video = Video::find(1); $tag1 = Tag::find(3); $tag2 = Tag::find(4); $video->tags()->attach(); $post = Post::find(1); $tag1 = Tag::find(3); $tag2 = Tag::find(4); $post->tags()->sync(); $video = Video::find(1); $tag1 = Tag::find(3); $tag2 = Tag::find(4); $video->tags()->sync(); I hope you will like the content and it will help you to learn Laravel Many to Many Polymorphic Relationship If you like this content, do share. Read the full article
#Laravelmanytomanypolymorphicrelations#LaravelmorphToManyexample#manytomanypolymorphicrelationsLaravel#polymorphicrelationseloquentLaravel
0 notes