我有两个桌子。 “星期”和“日期” 周表包含一个“ startDate”和一个“ endDate”。这两列都是指向日期列的foreignKeys。日期表包含一个主键ID和日期。
weeks | IDweeks | startDate | endDate | | 1 | 2 | 6 |
dates | IDdates | date | | 1 | 12-03-2018 | | 2 | 13-03-2018 | .....
查询星期时,我总是会获得startDate和endDate的键。我想从日期表中获取值。
我已经尝试着加载这样的值:
class weeks extends Model
{
//....
public function startDate()
{
return $this->belongsTo('App\date', 'startDate', 'date');
}
public function endDate()
{
return $this->belongsTo('App\date', 'endDate', 'date');
}
//....
class date extends Model
{
protected $primaryKey = 'IDdates';
public function showWeeks() {
$weeks = weeks::with('startDate')->get();
return view('weeks')->with('weeks', $weeks);
}
@foreach ($weeks as $week )
{{ $week->startDate->date }} // produces an error like: Trying to get property 'date' of non-object
{{ $week->startDate }} // works but returns only the keys instead of the value of dates table.
@endforeach
编辑:编辑标题中的错字