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

时间:2016-01-27 15:38:19

标签: php mysql laravel

我创建了三个表,即country table,countryevents表和country_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();

        });

最后一个是我的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修补匠并希望连接它们时,这是我的错误。

我确信我的模型是正确的,这是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();
    }
}

有人能告诉我那里的错误是什么吗?谢谢。

3 个答案:

答案 0 :(得分:2)

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

国家模型:

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

答案 1 :(得分:1)

问题是您将表country_countryevents称为country_country__events

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

了解更多信息,请查看this question

Convention table names (with underscore)

如果您不打算使用传统的表名,请注意将其添加到您的模型中:

public $table = 'your_table_name';

答案 2 :(得分:1)

确保数据库的名称与country_countryevents链接。那应该没问题。

相关问题