Laravel定义关系


Laravel Defining Relationships

我有一个名为Content的表,它是一个主表,如

Content : id content_name created_at updated_at

和另一个表Course,如

Course table have many content_id

Course : id content_id course_name created_at updated_at

我创建了这样的关系。

Content Model

class Content extends Eloquent {
    protected $table = 'contents';
    protected $guarded = array('id');
    public function course()
    {
        return $this->belongsTo('Course');
    }
}

Course Model

class Course extends Eloquent {
    protected $table = 'courses';
    protected $guarded = array('id');

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

当我像这样处理数据时$courses=Course::find(1)->content;

抛出类似

的错误

SQLSTATE[42S22]: Column not found: 1054未知列的内容。(SQL: select * from contents where contents .;)course_id = 1)

由于我刚到laravel,我无法纠正关系中的问题。

很接近,但你的关系倒了。拥有外键的表属于另一个表。在本例中,course表具有外键content_id,因此Course属于Content,而Content有一个或多个Course

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

在您的迁移(create_courses_table)中,确保像这样设置外键

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

我真的不太明白你的表格设计,也许它应该是这样的

取你的语句:"课程表有许多content_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();

$content = Content::with('course')->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;