[英]php call function as property
class Post extends Model
{
protected $fillable = ['title', 'body'];
public function comments()
{
return $this->hasMany(Comment::class);
}
}
Why when I call $post->comments() in php tinker; 为什么当我在PHP Tinker中调用$ post-> comments()时;为什么? it gives me this :Illuminate\\Database\\Eloquent\\Relations\\HasMany But when I remove the brackets from the comments word like: $post->comments , I get the array? 它给了我这个:Illuminate \\ Database \\ Eloquent \\ Relations \\ HasMany但是当我从诸如$ post-> comments之类的注释词中删除括号时,我得到了数组吗?
You shouldn't call $post->comments();
你不应该调用$post->comments();
, but $post->comments;
,但是$post->comments;
Also, your function name should be public function comments()
not public function comment()
. 另外,您的函数名称应为public function comments()
而不是public function comment()
。 You have many comment>s< for each post, not many comment. 您对每个帖子有很多comment> s <,没有很多评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.