【问题标题】:How to determine pivot-table name in belongToMany relationship in Laravel?如何确定 Laravel 中的 belongsToMany 关系中的数据透视表名称?
【发布时间】:2020-05-13 11:18:30
【问题描述】:

我遇到的问题是简单地尝试确定为 Laravel 中用于多对多关系的中间表提供正确的名称。

我在尝试访问 eloquent 关系时收到以下错误:

$product->类别; 使用消息'SQLSTATE [42S02] 照亮/数据库/查询异常:找不到基表或视图:1146 表 'dolstore-laravel.category_product' 不存在(SQL:选择 categories.*、category_product.product_id as pivot_product_id, category_product.category_id as pivot_category_id from categories inner join category_product on categories.id = @9876543@32@.category_productcategory_productcategory_productcategory_product 1)'

我的迁移是这样写的:

    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name')->unique();
            $table->timestamps();
        });

        Schema::create('product_category', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('product_id');
            $table->unsignedBigInteger('category_id');
            $table->timestamps();

            $table->unique(['product_id', 'category_id']);

            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
        });
    }

似乎我提供了不正确的名称 product_category,而应该是 category_product。管理这个的规则是什么?在另一个例子中,我实际上按照 Laracasts 教程成功,我的迁移是这样写的:

    public function up()
    {
        Schema::create('tags', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name')->unique();
            $table->timestamps();
        });
        //Pivot Table
        // article_tag

        Schema::create('article_tag', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('article_id');
            $table->unsignedBigInteger('tag_id');
            $table->timestamps();

            $table->unique(['article_id', 'tag_id']);

            $table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
            $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
        });
    }

因此,它似乎与创建表的顺序或定义数据透视表的迁移无关。

感谢您帮助我理解这一点。

【问题讨论】:

    标签: php laravel eloquent eloquent-relationship


    【解决方案1】:

    当您没有在模型中传递 belongsToMany 关系的第二个参数时。因此,Laravel 期望您在创建数据透视表时使用了它的命名约定。他们如何检查?假设您有一个产品和类别表。

    现在,laravel 会认为你已经在 按字母顺序,他们会期望您的数据透视表被命名 category_product 考虑到您的类别的第一个字母 这是“c”的第一个,而不是您的表格产品的第一个字母是“p”。 这就是为什么在您的 article_tag 数据透视表中它可以正常工作的原因?

    假设您通过不遵循其命名约定创建了您的数据透视表。那么你必须将它作为 m 到 m 关系中的第二个参数传递,就像这样。

    public function product_categoreis() [
       return $this->belongsToMany('App\Category', 'product_category');
    }
    

    【讨论】:

    • 完美答案。谢谢。
    猜你喜欢
    • 2014-12-15
    • 2021-03-30
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 2017-07-22
    • 2017-12-08
    • 1970-01-01
    • 2019-02-19
    相关资源
    最近更新 更多