我正在使用我目前手工制作的数据库和网站上的Illuminate(Laravel)进行首次练习。我想通过使用MVC进行改进,并发现Illuminate很有趣,可用于与数据库的交互。
我整天都在处理这些鲜血的小代码,找不到问题,我希望外面有人对我有一个好主意。非常感谢!
基本问题:为什么我不能迭代课程到给定的学期?虽然可以调用特定的课程。
use Illuminate\Database\Eloquent\Model as Eloquent;
class Semester extends Eloquent {
protected $table = "tblSemester";
protected $primaryKey = "SemesterID";
public function getCourses() {
// test if semester is correct
echo $this->SemesterID, " - ", $this->Semestertext, "<br>";
// This works fine and returns the expected result
$course = $this->hasMany('Course', 'Semester')->first();
echo $course->Number, " - ", $course->Title;
// This doesn't work. It returns nothing.
// There seems to be no data in $courses (just metadata)
$courses = $this->hasMany('Course', 'Semester');
foreach ($courses as $course) {
echo $course->Number, " - ", $course->Title;
echo "<br>";
}
return "<h1>" . "Test ends" . "</h1>";
}
}
非常感谢! 蒂姆
答案 0 :(得分:0)
首先
由于您说过您是'Laravel'的新手,并且想关注MVC,所以我想我可以给您一些建议。
示例模型
Threshold:The condition in which the alarm will go to the ALARM state.ConsumedReadCapacityUnits >= 6,000 for 5 datapoints within 5 minutes
Statistic: SamplesSum
Metric Name: ConsumedReadCapacityUnits
示例控制器
class Semester extends Model
{
protected $table = 'semesters';
// relationships
public function cources()
{
return $this->hasMany(Cource::class);
}
}
答案 1 :(得分:0)
这为什么不起作用:
public function cources()
{
return $this->hasMany(Cource::class);
}
但是这样做:
public function cources()
{
return $this->hasMany('Course', 'Semester')->get();
}