I wrote a code that sums the number of minutes of several videos together. But it shows the following error:
Attempt to read property "episodes" on null
public function setCourseTime($episode)
{
$course = $episode->course;
$course->time = $this->getCourseTime($course->episodes->pluck('time'));
$course->save();
}
Model Course.php
public function episodes()
{
return $this->hasMany(Episode::class);
}
2
Answers
Use
dd
for$course->episodes
, if is null use this:$course = $episode->course->load('episodes');
Try $course->episodes()->pluck(‘time’);