Laravel Eloquent 关系 "one to many",出现错误 "SQLSTATE[42S02]: Base table or view not found: 1146"

Laravel Eloquent Relationship "one to many", got an error "SQLSTATE[42S02]: Base table or view not found: 1146"

根据以下代码 我尝试在 "section" 和 "tours" 之间建立关系 但我得到了错误 Table 'yourproject.sections' doesn't exist (SQL: select * from 'sections' where 'sections'.'id' = 1 limit 1)

并且我使用 migrate 创建 table "section" 而不是 "sections"

注意:如果我向模型添加 table 名称,我会收到一个新错误

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

并且我使用 migrate 创建了一个列 "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;
});

因为您的外键列名称不遵循 Laravel 的命名约定,您必须指定它:

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

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

查看此 link 您需要了解的有关命名约定的所有信息以及 laravel

中的关系

http://daylerees.com/codebright/eloquent-relationships