Oh I forgot. The error occurs when different things happen, nothing to do with this name thing. It's a mystery.
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
Hi all. I do know what this error message means: you forgot () somewhere. But I can't find it anywhere inside my code, and the stack trace doesn't show me where I'm wrong...
LogicException in HasAttributes.php line 403: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
It refers to a variable or function called "name" in my User model, so something must be wrong here:
public function nameOrUsername()
{
if ($name = $this->name()) {
return $name;
} else {
return "@$this->username";
}
}
public function name()
{
if ($this->first_name || $this->last_name) {
return "$this->first_name $this->last_name";
} else {
return null;
}
}
It's like Twitter. A user has a username and it's displayed with a '@' in front of it, but it's stored without the at-sign. A user may or may not have a first name and/or last name. So when I want to welcome the user or show the name in the menubar I want to call nameOrUsername(). If the user has a first name and/or last name, that is returned. Else the username (with the @) is returned. I feel so stupid, I can't find the error. The error seemed to come up when pulled from my git repository after re-installing my OS. I tried PHP 5.6 and 7.0 while I was using PHp 7.1 before this, but that can't be the problem?
Where's the code where you're actually calling those methods?
I think the problem might be you need to make them attribute accessors. https://laravel.com/docs/5.4/eloquent-mutators#introduction
getNameAttribute() intead of name(), otherwise it thinks name() is a relationship (I believe). The same for nameOrUsername().
And you can put objects and arrays inside of a double quoted string. They just have to be surrounded by curly braces.
echo "This post belongs to {$user->name}";
echo "This post belongs to {$user['name']}";
Please or to participate in this conversation.