使用自定义表创建多对多态关系

时间:2019-11-28 05:30:35

标签: php laravel laravel-5 many-to-many polymorphic-relationship

如何使用自定义多态表创建多对多关系?

model_has_tags

$table->increments('id');
$table->integer('tag_id';
$table->string('model_type');
$table->integer('model_id');

标签

$table->increments('id');
$table->string('name');

用户

$table->increments('id');
$table->string('full_name');

我尝试了一下,但是没有用。

标记模型

public function users()
    {
        return $this->morphedByMany(User::class, 'model');
    }

用户模型

public function tags()
    {
        return $this->morphToMany(Tag::class, 'model');
    }

错误:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'stormport.models' doesn't exist (SQL: select `tags`.*, `models`.`model_id` as `pivot_model_id`, `models`.`tag_id` as `pivot_tag_id`....

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

找到了解决方案:

用户模型

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RDLCReport.Default" %>


<%@ Register assembly="Microsoft.ReportViewer.WebForms" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>




<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <rsweb:ReportViewer ID="ReportViewer1" runat="server">
        </rsweb:ReportViewer>
    </form>
    </body>
</html>