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 Illuminate\Database\Eloquent\Model;

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 Illuminate\Database\Eloquent\Model;

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 Illuminate\Database\Eloquent\Model;

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...

Laravel Many to Many Polymorphic Relationship的更多相关文章

  1. Laravel Vuejs 实战:开发知乎 (9)定义话题与问题关系

    1.话题[Topic] 执行命令: php artisan make:model Topic –cmr 修改****_**_**_create_topics_table.php数据库迁移文件如下: c ...

  2. laravel model relationship

    laravel支持多种模型之间的relation,对应着模型间的one2one, one2many,many2many,hasManyThrough,Polymorphic, many2many po ...

  3. Laravel Relationship Events

    Laravel Relationship Events is a package by Viacheslav Ostrovskiy that adds extra model relationship ...

  4. Laravel Eloquent ORM

    Eloquent ORM 简介 基本用法 集体赋值 插入.更新.删除 软删除 时间戳 查询范围 关系 查询关系 预先加载 插入相关模型 触发父模型时间戳 与数据透视表工作 集合 访问器和调整器 日期调 ...

  5. PHP and laravel知识点小小积累

    function () use ($x, &$y){} 自从PHP5.3开始有了closure/匿名函数的概念,在这里的use关键词的作用是允许匿名函数capture到父函数scope 内存在 ...

  6. Laravel五大功能之Eloquent关系模式

    Eloquent是Laravel的原始ActiveRecord是实现的,建立在Laravel的Fluent Query Builder之上的,所以Eloquent类和Fluent类是一样的,能实现复杂 ...

  7. Laravel教程 八:queryScope 和 setAttribute

    Laravel教程 八:queryScope 和 setAttribute 此文章为原创文章,未经同意,禁止转载. Laravel Eloquent Database 直接就是按照上一节所说的那样,我 ...

  8. laravel速记(笔记)

    命令行: php artisan controller:make UserController This will generate the controller at /app/controller ...

  9. laravel Authentication and Security

    Creating the user modelFirst of all, we need to define the model that is going to be used to represe ...

随机推荐

  1. QQ传文件测试要点

    总-分-总 UI:   进度:进度条.百分比.速度.已传文件大小 显示传送文件图标.悬浮有文字 功能入口:图标.菜单项 各种提示:开始传送.各种异常信息的提示.传送结束 给好友传文件.给群传文件 功能 ...

  2. aspectj ----- 简介

    一.为什么写这个系列的博客   Aspectj一个易用的.功能强大的aop编程语言.其官网地址是:http://www.eclipse.org/aspectj/,目前最新版本为:1.7.0 RC1.但 ...

  3. react+webpack+babel环境搭建

    [react+webpack+babel环境搭建] 1.react官方文档推荐使用 babel-preset-react.babel-preset-es2015 两个perset. Babel官方文档 ...

  4. 简单web测试流程(转载)

    转载自 http://blog.csdn.net/qq_35885203 1.界面操作模式打开jmeter 进入jmeter安装目录的bin目录下,双击“jmeter.bat”文件即可打开jmeter ...

  5. Python基础之列表及元组的增删改查

    定义一个列表 晚上的状态不太适合写博客,容易犯困~ 列表的增加有三种方式 第一种主流的增加 append函数 list1 = ['noevil', 'fordearme', 'city', 'cust ...

  6. 第十章 优先级队列 (xa2)左式堆:合并

  7. 第四章 栈与队列(c3)栈应用:栈混洗

  8. 最小生成树算法(krustra+prime)

    给你一个图,求让图连通的边权和最小值 krustra算法是基于加边法,将所有边权排序,每次加一条边,将两个点放在同一个集合中.如果新加的点不在同一个集合中,就合并(并查集) 涉及到排序,可以用结构体存 ...

  9. Bootstrap(7) 输入框和导航组件

            一.输入框组件 文本输入框就是可以在<input>元素前后加上文字或按钮,可以实现对表单控件的扩展. //在左侧添加文字 <!-- //在左侧添加文字 --> ...

  10. sqlserver window身份验证时切换账户的快捷键

    sqlserver window身份验证时切换账户的快捷键:ctrl+alt_del