Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to undefined relationship in laravel

Tags:

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');
}
like image 499
KaziBablu Avatar asked Dec 20 '17 12:12

KaziBablu


2 Answers

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.

like image 184
Gothiquo Avatar answered Oct 08 '22 01:10

Gothiquo


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.

like image 20
Atiqur Avatar answered Oct 08 '22 01:10

Atiqur