I have a Model method where I am trying to return a start and end date.
So like this:
public function start_end_date()
{
$instances = $this->hasMany(Instance::class, 'instance_id')
->where('instance_id', '=', $this->id)
->where('weekstart', '>=', time());
$dates = $instances->pluck('weekstart')->toArray();
$dates = array_map('strtotime', $dates);
$startdate = min($dates);
$enddate = strtotime("+7 day", max($dates));
return array('startdate' => $startdate, 'enddate' => $enddate);
}
In my view, I have $courses passed to the view. This object returns courseinstances via its model, as
public function courseinstances()
{
return $this->hasMany(CcoursesInstance::class, 'course_id')
->where('startdate', '>=', time());
}
So in view I can loop over $courses (e.g. @foreach $courses as $course) and also loop over instances (e.g. @foreach $course->courseinstances as @instance) and that works fine. For instance I can access some field or method-returned value as $instance->somedata
But in the view where I have $course->courseinstances->start_end_date->startdate
Laravel says
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation (View:
I just can't figure out how Laravel wants me to return the start and end date and then reference it in the view. I don't want to pass separate variables to the view. I want to just use $courses as passed to the view and then drill down as I do for other pieces of data related to a course.
Ideas?
Thanks, Brian