I have two models I am trying to create a one to one relationship for: A Week model and WeekType model. The WeekType belongs to Week, and should be a one to one relationship.. however, Laravel is returning null on a dd(Week::find($id)->weektpye);
Here are my models:
Week.php
public function weekTypes()
{
return $this -> hasOne('App\WeekType');
}
WeekType.php
public function weeks()
{
return $this -> belongsTo('App\Week');
}
And my schema:
I feel I'm missing something obvious or my schema is incorrectly configured, though I'm not sure how.
I think you might be mixing up singular and plural naming. It looks like you are trying to access the singular version in your dd(), but have named it plural. Also, there is a typo in the dd(). Your Schema looks okay, so I believe the following should work for you.
Week.php
public function weekType()
{
return $this->hasOne('App\WeekType', 'week_type_id');
}
WeekType.php
public function weeks()
{
return $this->belongsTo('App\Week', 'week_type_id');
}
After that the following should work for you.
dd(Week::find($id)->weekType);