Php Laravel数据库错误
目前,我正在与拉威尔一起制作一个票务系统。 但我对数据库结构不太熟悉 我制作了两张带有迁移的表 称为: 门票, 评论 显然,一张票证可以有多条评论,但一条评论只能有一张票证。所以这是一对多的关系 在车票模型中,我声明:Php Laravel数据库错误,php,laravel,orm,laravel-5,eloquent,Php,Laravel,Orm,Laravel 5,Eloquent,目前,我正在与拉威尔一起制作一个票务系统。 但我对数据库结构不太熟悉 我制作了两张带有迁移的表 称为: 门票, 评论 显然,一张票证可以有多条评论,但一条评论只能有一张票证。所以这是一对多的关系 在车票模型中,我声明: class tickets extends Model { protected $fillable = ['title', 'content', 'slug', 'status', 'user_id']; public function comments()
class tickets extends Model
{
protected $fillable = ['title', 'content', 'slug', 'status', 'user_id'];
public function comments()
{
return $this->hasMany('App\comments', 'post_id');
}
}
class comments extends Model
{
protected $guarded = ['id'];
public function ticket()
{
return $this->belongsTo('App\tickets');
}
}
在评论模型中,我声明:
class tickets extends Model
{
protected $fillable = ['title', 'content', 'slug', 'status', 'user_id'];
public function comments()
{
return $this->hasMany('App\comments', 'post_id');
}
}
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:未定义的属性:
Illumb\Database\Eloquent\Relations\HasMany::$get
我做错了什么
谢谢。当我认为您的目的是调用
get()
方法时,您似乎正在尝试访问get
属性。替换此项:
$comments = $ticket->comments()->get;
与
哇,太好了!认真对待这个问题!!谢谢