【发布时间】:2015-10-07 14:38:53
【问题描述】:
目前我正在使用 laravel 制作票务系统。 但我对数据库结构不太熟悉。
我已经制作了两张带有迁移的表。
调用:
门票, 评论
很明显,一张票可以有多个 cmets,但一条评论只能有一张票。所以这是一对多的关系。
在门票模型中,我声明了这一点:
class tickets extends Model
{
protected $fillable = ['title', 'content', 'slug', 'status', 'user_id'];
public function comments()
{
return $this->hasMany('App\comments', 'post_id');
}
}
在 Comments 模型中,我声明了这一点:
class comments extends Model
{
protected $guarded = ['id'];
public function ticket()
{
return $this->belongsTo('App\tickets');
}
}
这是我的控制器:
public function show($slug)
{
$ticket = tickets::whereSlug($slug)->firstOrFail();
$comments = $ticket->comments()->get;
return view('tickets.show',compact('ticket','comments'));
}
执行此操作时,我收到错误:
TicketController.php 第 77 行中的 ErrorException:未定义属性: Illuminate\Database\Eloquent\Relations\HasMany::$get
我做错了什么?
谢谢。
【问题讨论】:
标签: php laravel orm laravel-5 eloquent