Php laravel中的多对多关系需要表的特定名称吗?

Php laravel中的多对多关系需要表的特定名称吗?,php,mysql,laravel,Php,Mysql,Laravel,我创建了三个表,即国家表、countryevents表和country\u countryevents表。 我想知道的第一个问题是,需要表的特定名称吗 以下是我创建表的方式: 第一个是国家表: Schema::create('country', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $tab

我创建了三个表,即国家表、countryevents表和country\u countryevents表。 我想知道的第一个问题是,需要表的特定名称吗

以下是我创建表的方式: 第一个是国家表:

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

        });
第二个是countryevents表:

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

        });
最后一个是my country\u 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();
    }
}

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

国家模式:

public function country_events(){
    return $this->belongsToMany(
        'App\Country_Events',
        'country_countryevents',
        'country_id',
        'country_events_id'
    )->withTimestamps();
}
国家/地区活动模式:

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

问题在于您将表
country\u countryevents
作为
country\u country\u events

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

欲了解更多信息,请查看

如果您不打算使用常规表名,请记住将其添加到您的模型中:

public $table = 'your_table_name';

确保数据库名称与您的国家/地区链接。然后它应该可以正常工作。

问题是引用表
country\u countryevents
作为
country\u country\u events
嗨,我尝试了你的方法,然后它没有显示错误,但现在它变成了另一个错误,那就是
PHP致命错误:在eval()'d代码第1行调用未定义的函数attach()
,请在调用attach()的地方包含代码。是的,我把$table放在模型中了。
public $table = 'your_table_name';