skip to Main Content

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


  1. Use dd for $course->episodes, if is null use this:
    $course = $episode->course->load('episodes');

    Login or Signup to reply.
  2. Try $course->episodes()->pluck(‘time’);

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search