KaziBablu
KaziBablu

Reputation: 533

Call to undefined relationship in laravel

I have two tables departments and course, and I have defined a relationship between them but I received this error message:

Call to undefined relationship in laravel

Can anyone tell me please where's the problem?

Model Code:

public function course()
{
    return $this->hasMany('App\Course','departments_id','id');
}

Model Department:

public function department()
{
    return $this->belongsTo('App\Departments','departments_id');
}

Upvotes: 11

Views: 43163

Answers (3)

saber tabatabaee yazdi
saber tabatabaee yazdi

Reputation: 4959

in my case, i used:

::with('boards')

i just changed it to :

::with(['boards'])

with brackets

in my department Model:

public function boards()
{
    return $this->hasMany(Board::class);
}

Upvotes: 6

Atiqur
Atiqur

Reputation: 4022

First of all little correction in schema $table->integer('department_id'); should be $table->unsignedInteger('department_id');

And the problem is - in your migration column name is department_id but in your relation you have written departments_id there is a extra S!

just correct the column name from model.

Upvotes: 3

Gothiquo
Gothiquo

Reputation: 868

So problem is so clear. Either in your Course model you have to rename the function department to departments, or to make a new one with the correct name.

Upvotes: 6

Related Questions