2015-02-09 83 views
0

根據以下代碼 我試圖創建「部分」和「旅遊」 但我得到的錯誤 Table 'yourproject.sections' doesn't exist (SQL: select * from 'sections' where 'sections'.'id' = 1 limit 1)

我用遷移到創建表「部分」而不是「部分」Laravel雄辯的關係「一對多」,得到一個錯誤「SQLSTATE [42S02]:基表或視圖未找到:1146」

注:如果我添加一個表名的模型我得到一個新的錯誤

Column not found: 1054 Unknown column 'tours.section_id' in 'where clause' (SQL: select * from `tours` where `tours`.`section_id` = 1) 

我用m igrate以創建一個列 「sectionid」 而不是 「SECTION_ID」

型號 Section.php

<?php 
class Section extends Eloquent{ 
    protected $table = 'section'; 
    protected $fullable = array('tit'); 
    public function tours(){ 
     return $this -> hasMany('Tours'); 
    } 
} 

Tours.php

<?php 
class tours extends Eloquent{ 
    protected $fullable = array('tit','sectionid'); 
    public function section(){ 
     return $this -> belongsTo('Section'); 
    } 
} 

遷移 科

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateSectionTable extends Migration { 

    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('section',function($table){ 
      $table->increments('id'); 
      $table->string('tit'); 
      $table->timestamps(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::drop('section'); 
    } 

} 

旅遊

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateToursTable extends Migration { 

    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('tours',function($table){ 
      $table->increments('id'); 
      $table->string('tit'); 
      $table->timestamps(); 
      $table->integer('sectionid')->unsigned(); 
      $table->foreign('sectionid')->references('id')->on('section'); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::drop('tours'); 
    } 

} 

路線

<?php 

/* 
|-------------------------------------------------------------------------- 
| Application Routes 
|-------------------------------------------------------------------------- 
| 
| Here is where you can register all of the routes for an application. 
| It's a breeze. Simply tell Laravel the URIs it should respond to 
| and give it the Closure to execute when that URI is requested. 
| 
*/ 

Route::get('/', function() 
{ 
$hotes = Section::find(1)->tours()->get(); 
return $hotes->tit; 
}); 

回答

0

因爲你的外鍵列名不遵循Laravel的命名規則,你必須指定它:

public function tours(){ 
    return $this->hasMany('Tours', 'sectionid'); 
} 

而且

public function section(){ 
    return $this->belongsTo('Section', 'sectionid'); 
} 
+0

我加在遷移文件中的外鍵'$表 - >整數( 'sectionid') - >無符號(); $ table-> foreign('sectionid') - >引用('id') - > on('section');' 並且還會在關係方法中添加'sectionid'並且仍然存在錯誤 – 2015-02-09 14:31:28

+0

什麼錯誤你得到了嗎? – lukasgeiter 2015-02-09 14:33:17

+0

SQLSTATE [42S02]:找不到基表或視圖:1146'yourproject.sections'不存在(SQL:select * from'sections'其中'sections'.'id' = 1的限制1) – 2015-02-09 14:38:57

相關問題