【发布时间】:2020-04-14 17:10:10
【问题描述】:
我尝试学习 laravel 并做一些测试/演示应用程序。我现在一直在努力解决 laravel/eloquent 表的关系。我需要建议。
我有 3 个模型 [Application, Term, AppState] 及其表 applications[id, terms_id, appStates_id, and other cols], terms[id, startDate, endDate, ...], app_states[id, caption]
Application.php
public function term()
{
return $this->belongsTo('App\Term');
}
public function appState()
{
return $this->belongsTo('App\AppState');
}
在 Term.php 和 AppState.php 我有:
public function applications()
{
return $this->hasMany('App\Application');
}
如何在刀片中为每个应用程序说“caption”/“startDay”+“endDate”?我可以在 foreach 循环中获取他们的 ID $app->terms_id/$app->appStates_id,但我想从 app_states 表中获取 caption 值。
必须在迁移中也指定这种关系吗?在某些 tuts 中提到,如果我只想在 laravel 中处理它,则不需要。
感谢您的建议
【问题讨论】: