zoukankan      html  css  js  c++  java
  • Laravel Many to Many Polymorphic Relationship

    Many to many Polymorphic relationship is also a little bit complicated to understand. For example, if you have posts, videos, and tag tables, you require to connect with each other with your requirement like every post have multiple tags and same for videos too. Also every tag many are connected with multiple post or multiple videos too. But we can easily do it using just one table "taggables". Just read the article and you got it.

    In this article, you can understand how to create polymorphic many-to-many relationships with migration with a foreign key schema for one to many relationships, use sync with a pivot table, create records, attach records, get all records, delete, update, where condition and everything related to many to many polymorphic relationship.

    In this example, i will create "posts", "videos", "tags" and "taggables" tables. each table is connected with each other. now we will create many to many polymorphic relationships with each other by using laravel Eloquent Model. We will first create database migration, then model, retrieve records and then how to create records too. So you can also see database table structure on below screen.

    Polymorphic Many to Many Relationship will use "morphToMany()" and "morphedByMany()" for relation.

    Create Migrations:

    Now we have to create migration of "posts", "videos", "tags" and "taggables" table. so let's create like as below:

    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");

    });

    Create Models:

    Here, we will create Post, Video and Tag table model. we will also use "morphToMany()" and "morphedByMany()" for relationship of both model.

    Post Model:

    <?php

    namespace App;

    use IlluminateDatabaseEloquentModel;

    class Post extends Model

    {

    /**

    * Get all of the tags for the post.

    */

    public function tags()

    {

    return $this->morphToMany(Tag::class, 'taggable');

    }

    }

    Video Model:

    <?php

    namespace App;

    use IlluminateDatabaseEloquentModel;

    class Video extends Model

    {

    /**

    * Get all of the tags for the post.

    */

    public function tags()

    {

    return $this->morphToMany(Tag::class, 'taggable');

    }

    }

    Tag Model:

    <?php

    namespace App;

    use IlluminateDatabaseEloquentModel;

    class Tag extends Model

    {

    /**

    * Get all of the posts that are assigned this tag.

    */

    public function posts()

    {

    return $this->morphedByMany(Post::class, 'taggable');

    }

    /**

    * Get all of the videos that are assigned this tag.

    */

    public function videos()

    {

    return $this->morphedByMany(Video::class, 'taggable');

    }

    }

    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);

    Create Records:

    $post = Post::find(1);

    $tag = new Tag;

    $tag->name = "ItSolutionStuff.com";

    $post->tags()->save($tag);

    $video = Video::find(1);

    $tag = new Tag;

    $tag->name = "ItSolutionStuff.com";

    $video->tags()->save($tag);

    $post = Post::find(1);

    $tag1 = new Tag;

    $tag1->name = "ItSolutionStuff.com";

    $tag2 = new Tag;

    $tag2->name = "ItSolutionStuff.com 2";

    $post->tags()->saveMany([$tag1, $tag2]);

    $video = Video::find(1);

    $tag1 = new Tag;

    $tag1->name = "ItSolutionStuff.com";

    $tag2 = new Tag;

    $tag2->name = "ItSolutionStuff.com 2";

    $video->tags()->saveMany([$tag1, $tag2]);

    $post = Post::find(1);

    $tag1 = Tag::find(3);

    $tag2 = Tag::find(4);

    $post->tags()->attach([$tag1->id, $tag2->id]);

    $video = Video::find(1);

    $tag1 = Tag::find(3);

    $tag2 = Tag::find(4);

    $video->tags()->attach([$tag1->id, $tag2->id]);

    $post = Post::find(1);

    $tag1 = Tag::find(3);

    $tag2 = Tag::find(4);

    $post->tags()->sync([$tag1->id, $tag2->id]);

    $video = Video::find(1);

    $tag1 = Tag::find(3);

    $tag2 = Tag::find(4);

    $video->tags()->sync([$tag1->id, $tag2->id]);

    I hope you understand of many to many relationship...

  • 相关阅读:
    【转载】 卷积神经网络(Convolutional Neural Network,CNN)
    【转载】 深度学习之卷积神经网络(CNN)详解与代码实现(一)
    【边缘计算】 Edge Computing: Vision and Challenges
    【转载】 一种替代性的基于模拟的搜索方法,即策略梯度搜索
    【转载】 共享相关任务表征,一文读懂深度神经网络多任务学习
    【转载】 另一种(深度)学习:自我监督学习会是下一个重点导向吗?
    【转载】 谷歌做了个机器人,扔东西比人准多了 | 极客酷玩
    【节选 转载】 当前的迁移学习算法进行分类
    【转载】 第四范式首席科学家杨强:AlphaGo的弱点及迁移学习的应对(附视频)
    Flash打开新窗口 被浏览器拦截问题 navigateToURL被拦截 真正试验结果
  • 原文地址:https://www.cnblogs.com/mouseleo/p/10080293.html
Copyright © 2011-2022 走看看