I'm working on a project with Laravel and this function gives me an error. What I want to get is the name of the trainer who gives the course.
<span class="title">{{ $course->owner->specialization->getName() }}</span>
The error that appears is the following:
Relationship method must return an object of type
Illuminate \ Database \ Eloquent \ Relations \ Relation
And so the functions in the classes are defined:
Inside the Courses class
public function owner()
{
return $this->belongsTo('App\User', 'user_id', 'id')->getResults();
}
Inside the User class
public function specialization()
{
if($this->isTrainer()) return $this->trainer();
if($this->isCenter()) return $this->center();
return null;
}
private function trainer()
{
return $this->hasOne('App\Trainer');
}
Inside the Trainer class
public function getName()
{
$name = '';
if(!empty($this->name)) {
$name .= $this->name;
}
if(!empty($this->last_name)) {
$name .= ' ' . $this->last_name;
}
if(!empty($this->second_name)) {
$name .= ' ' . $this->second_name;
}
return $name;
}