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

标签 php mysql laravel-4 migration eloquent

根据以下代码 我尝试在“部分”和“游览”之间建立关系 但我得到了错误 表“yourproject.sections”不存在(SQL:select * from 'sections' where 'sections'.'id' = 1 limit 1)

我使用 migrate 创建表“section”而不是“sections”

注意:如果我将表名添加到模型中,我会收到一个新错误

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”

型号 部分.php

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

关于php - Laravel Eloquent 关系 "one to many",出现错误 "SQLSTATE[42S02]: Base table or view not found: 1146",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28411507/

相关文章:

php - 使用onclick查询MySQL,在不刷新的情况下在同一页面上显示结果

php - 简单的 PHP RSVP 表单

php - 从 Controller 调用外部 API 函数,LARAVEL 4

laravel-4 - Codeception API 测试 : stuck at passing json payload to REST service

php - 在 PHP 4 中解析和字符串化 JSON

php - 选择在包含多个以逗号分隔的 id 的列中搜索某个 id 的行

php - 将额外模型附加到 BelongstoMany 关系

php - Blob 还是上传?

php - 什么时候*不*使用准备好的语句?

php - Laravel Response::json() 带数字检查