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');
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With