Reputation: 533
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
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
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
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