php - Many to many relationship in laravel need a specific name for the table? -
i have created 3 table country table, countryevents table, , country_countryevents table. first question know, need specific name table?
here how create table: first 1 country table:
schema::create('country', function (blueprint $table) { $table->increments('id'); $table->string('name'); $table->timestamps(); });
the second 1 countryevents table :
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(); });
the last 1 country_countryevents table:
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(); });
i not sure going on code here, cause cant attach event country.
illuminate\database\queryexception message 'sqlstate[42s02]: base table or view not found: 1146 table 'calendar.country_country__events' doesn't exist (sql: insert `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))'
this error when run php artisan tinker, , want connect them.
i sure model correct, here country.php model:
<?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(); } }
this 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(); } }
could tell me error there?thanks.
yes, need reference table name , foreign , local keys well.
country model:
public function country_events(){ return $this->belongstomany( 'app\country_events', 'country_countryevents', 'country_id', 'country_events_id' )->withtimestamps(); }
country_events model:
public function country(){ return $this->belongstomany( 'app\country', 'country_countryevents', 'country_events_id', 'country_id' )->withtimestamps(); }
Comments
Post a Comment