Php 拉威尔定义关系

Php 拉威尔定义关系,php,laravel,laravel-4,relationships,table-relationships,Php,Laravel,Laravel 4,Relationships,Table Relationships,我有一个名为Content的表,它是一个类似于 Content:id-Content\u-name-created\u-at-updated\u-at 还有另一张表Courselike 课程表有许多内容\u id 课程:id内容\u id课程\u名称在更新时创建\u在 我创造了这样的关系 内容模型 class Content extends Eloquent { protected $table = 'contents'; protected $guarded = array(

我有一个名为
Content
的表,它是一个类似于

Content:id-Content\u-name-created\u-at-updated\u-at

还有另一张表
Course
like

课程表有许多内容\u id

课程:id内容\u id课程\u名称在更新时创建\u在

我创造了这样的关系

内容模型

class Content extends Eloquent {

    protected $table = 'contents';
    protected $guarded = array('id');

    public function course()
    {
        return $this->belongsTo('Course');
    }
}
class Course extends Eloquent {

    protected $table = 'courses';
    protected $guarded = array('id');


    public function content()
    {
        return $this->hasMany('Content');
    }
}
课程模式

class Content extends Eloquent {

    protected $table = 'contents';
    protected $guarded = array('id');

    public function course()
    {
        return $this->belongsTo('Course');
    }
}
class Course extends Eloquent {

    protected $table = 'courses';
    protected $guarded = array('id');


    public function content()
    {
        return $this->hasMany('Content');
    }
}
当我像这样处理数据时
$courses=课程::查找(1)->内容

它抛出错误,就像

SQLSTATE[42S22]:未找到列:“where子句”中的1054未知列“contents.course\u id”(SQL:select*from
contents
where
contents
course\u id
=1)

我无法纠正关系中的问题,因为我对laravel是新手。

在您的迁移(创建课程表)中,请确保像这样设置外键

$table->integer('content_id)->unsigned()->index();
            $table->foreign('content_id')
                  ->references('id')
                  ->on('contents')
                  ->onDelete('cascade');

亲密,但你的关系倒退了。具有外键的表是属于另一个表的表。在这种情况下,您的
课程
表具有外键
内容_id
,因此
课程
属于
内容
,而
内容
具有一个或多个
课程
s

class Content extends Eloquent {
    public function course() {
        return $this->hasMany('Course');
    }
}

class Course extends Eloquent {
    public function content() {
        return $this->belongsTo('Content');
    }
}

我不太明白你的桌子设计,也许应该是这样

接受您的陈述:“课程表有许多内容\u id”。我知道你是说一门课程可以有多种内容,对吗?如果是,您可能希望将表格设计更改为如下所示

Course
======
id
course_name
created_at 
updated_at

Content
=======
id
course_id (set this as FK)
content_name 
created_at 
updated_at
内容的迁移代码

public function up()
{
    Schema::create('content', function(Blueprint $table)
    {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('course_id')->unsigned();
        $table->string('content_name');
    });

    Schema::table('content',function($table)
    {
        $table->foreign('course_id')->references('id')->on('course')->onDelete('cascade');
    });
}
然后在你的模型中

class Course extends Eloquent
{
    protected $table = 'course';

    public function content()
    {
        return $this->hasMany('content', 'course_id', 'id');
    }
}

class Content extends Eloquent
{
    protected $table = 'content';

    public function course()
    {
        return $this->belongsTo('course', 'course_id', 'id');
    }
}
然后通过即时加载访问您的数据

$course = Course::with('content')->get();


这是关于确定关联的。你的联系应该是:

内容 内容有很多课程

class Content extends Eloquent {

    protected $table = 'contents';
    protected $guarded = array('id');

    public function courses()
    {
        return $this->hasMany('Course');
    }
}
课程

这门课属于内容

class Course extends Eloquent {

    protected $table = 'courses';
    protected $guarded = array('id');


    public function content()
    {
        return $this->belongsTo('Content');
    }
}
所以你可以做查询关联。 查找内容->课程:

$courses = Content::find(1)->courses;
查找课程->内容:

$content = Course::find(1)->content;

为什么
课程
属于
课程
并且
内容
有许多
内容
?那么可能的解决方案是什么:(请帮助我)数据库的内容表中是否有课程id列?内容表中没有,只有这些字段(id内容名称在更新时创建)干杯!你的回答节省了我的时间!