【问题标题】:Many to many relationship in laravel need a specific name for the table?laravel 中的多对多关系需要表的特定名称吗?
【发布时间】:2016-05-04 15:46:58
【问题描述】:

我创建了三个表,即国家表、国家事件表和国家国家事件表。 我想知道的第一个问题,是否需要表的具体名称?

这是我创建表格的方式: 第一个是国家表:

 Schema::create('country', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();

        });

第二个是国家事件表:

 Schema::create('countryevents', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('description');
            $table->string('start');
            $table->string('end');
            $table->string('type');
            $table->timestamps();

        });

最后一张是我的 country_countryevents 表:

   Schema::create('country_countryevents', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('country_id')->unsigned();
            $table->foreign('country_id')->references('id')->on('country')->onDelete('cascade');

            $table->integer('country_events_id')->unsigned();
            $table->foreign('country_events_id')->references('id')->on('countryevents')->onDelete('cascade');
            $table->integer('editable');
            $table->timestamps();


        });

我不确定我的代码在这里发生了什么,因为我无法将事件附加到我的国家/地区。

Illuminate\Database\QueryException with message 'SQLSTATE[42S02]: Base table or view
not found: 1146 Table 'calendar.country_country__events' doesn't exist (SQL: insert into `cou
ntry_country__events` (`country__events_id`, `country_id`, `created_at`, `updated_at`) values
 (1, 1, 2016-01-27 15:31:03, 2016-01-27 15:31:03))'

这是我在运行 php artisan tinker 并想要连接它们时的错误。

我确定我的模型是正确的,这里是 Country.php 模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Country extends Model
{
    //
    protected $table='country';


    public function country_events(){
        return $this->belongsToMany('App\Country_Events')->withTimestamps();
    }
}

这是我的 Country_Events.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Country_Events extends Model
{
    protected $table='countryevents';


    public function country(){
        return $this->belongsToMany('App\Country')->withTimestamps();
    }
}

谁能告诉我我的错误是什么?谢谢。

【问题讨论】:

  • 问题是表country_countryevents作为country_country__events的引用

标签: php mysql laravel


【解决方案1】:

确保数据库的名称与您的 country_countryevents 链接。那么它应该可以正常工作。

【讨论】:

    【解决方案2】:

    问题是您将表 country_countryevents 引用为 country_country__events

    检查数据库中的表名,看看它最终是如何创建这个表的。

    更多信息请查看this question

    Convention table names (with underscore)

    如果您不打算使用传统的表名,请记住将此添加到您的模型中:

    public $table = 'your_table_name';
    

    【讨论】:

    • 是的,我把 $table 放到了模型里面。
    【解决方案3】:

    是的,您需要引用表名,可能还需要引用外键和本地键。

    国家模式:

    public function country_events(){
        return $this->belongsToMany(
            'App\Country_Events',
            'country_countryevents',
            'country_id',
            'country_events_id'
        )->withTimestamps();
    }
    

    Country_Events 模型:

    public function country(){
        return $this->belongsToMany(
            'App\Country',
            'country_countryevents',
            'country_events_id',
            'country_id'
        )->withTimestamps();
    }
    

    【讨论】:

    • 嗨,我尝试了你的方法然后它没有显示错误但现在它变成另一个错误是PHP Fatal error: Call to undefined function attach() in eval()'d code o n line 1
    • 请在调用 attach() 的位置包含您的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 2016-04-26
    相关资源
    最近更新 更多