php调用函数作为属性

时间:2017-04-26 07:10:46

标签: php laravel

class Post extends Model
{
    protected $fillable = ['title', 'body'];
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

为什么我在php tinker中调用$ post-> comments();它给了我:Illuminate \ Database \ Eloquent \ Relations \ HasMany 但是当我从评论词中删除括号时:$ post-> comments,我得到了数组?

1 个答案:

答案 0 :(得分:0)

您不应该致电$post->comments();,而是$post->comments;

此外,您的函数名称应为public function comments()而不是public function comment()。你有很多评论> s<对于每个帖子,评论不多。