Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

iMyque's avatar

Eloquent Dynamic Properties not fetching - L5

Hello Jeff,

Thanks for this tutorial, It has made my sojourn into Laravel much more easier.

I have an issue with Eloquent, When I try to access an attribute of the related User object from an Article object like this;

$article = Article::findOrFail($id);
$article->user->name;

I get this error, what could be wrong?:

LogicException in Model.php line 2641:
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
in Model.php line 2641
at Model->getRelationshipFromMethod('user') in Model.php line 2572
at Model->getAttribute('user') in Model.php line 3231
at Model->__get('user') in ArticlesController.php line 129
at ArticlesController->show(object(Article))

--- Models ---

namespace App\Models;
class Article extends Model {
    public function user(){
        $this->belongsTo('App\User');
    }
}

namespace App;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
    public function articles(){
        return $this->hasMany('App\Models\Article');
     }
}

--- Migration ---

Schema::table('articles', function(Blueprint $table) {
    ...
    $table->integer('user_id')->unsigned();        
    $table->foreign('user_id')
        ->references('id')
        ->on('users')
        ->onDelete('cascade');
});
0 likes
3 replies
michaeldyrynda's avatar
Level 41

In your Article model, you aren't returning the relationship in the user method.

class Article extends Model {
    public function user()
    {
        $this->belongsTo('App\User');
    }
}

This should be

class Article extends Model {
    public function user()
    {
        return $this->belongsTo('App\User');
    }
}
2 likes
iMyque's avatar

Thank you deringer.

You just saved me hours of debugging.

Please or to participate in this conversation.