afzylive
afzylive

Reputation: 21

Laravel Defining Relationships

I have one table named Content which is a master table like

Content : id content_name created_at updated_at

and another table Course like

Course table have many content_id

Course : id content_id course_name created_at updated_at

I have created relation like this.

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');
    }
}

When i am fething the data like this $courses=Course::find(1)->content;

It throws error like

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'contents.course_id' in 'where clause' (SQL: select * from contents where contents.course_id = 1)

I am unable to rectify the problem in relations as I am new to laravel.

Upvotes: 1

Views: 74

Answers (4)

akbarbin
akbarbin

Reputation: 5105

This is about determining associations. Your associations should be:

Content Content has many courses

class Content extends Eloquent {

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

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

Course

The course belongs to content.

class Course extends Eloquent {

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


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

So you can do query association. For finding content -> courses:

$courses = Content::find(1)->courses;

For finding course -> content:

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

Upvotes: 0

rfpdl
rfpdl

Reputation: 964

I don't really understand your table design, maybe it should be something like this.

Taking your statement: "Course table have many content_id". I perceive that you are saying that 1 course can have multiple content, is that right? If yes, you might want to change your table design to something like below

Course
======
id
course_name
created_at 
updated_at

Content
=======
id
course_id (set this as FK)
content_name 
created_at 
updated_at

migration code for content

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');
    });
}

Then in your model

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');
    }
}

Then to access your data via eager loading

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

OR

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

Upvotes: 0

patricus
patricus

Reputation: 62368

Close, but you have your relationships backwards. The table that has the foreign key is the one that belongsTo the other one. In this case, your course table has the foreign key content_id, therefore Course belongs to Content, and Content has one or many Courses.

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

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

Upvotes: 1

Abdelrahman Magraby
Abdelrahman Magraby

Reputation: 1272

in your migrations(create_courses_table) , make sure to set the foreign key like that ,

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

Upvotes: 0

Related Questions