Currently I'm making a ticket system with laravel. But I'm not quite familiar with the database structure.
I've made two tables with migrations.
Called:
Tickets, Comments
Obvisously a ticket can have multiple comments but a comment can only have one ticket. So it's a one to many relationship.
In the model of Tickets I've declared this:
class tickets extends Model
{
protected $fillable = ['title', 'content', 'slug', 'status', 'user_id'];
public function comments()
{
return $this->hasMany('App\comments', 'post_id');
}
}
In the model of Comments I've declared this:
class comments extends Model
{
protected $guarded = ['id'];
public function ticket()
{
return $this->belongsTo('App\tickets');
}
}
And this is my controller:
public function show($slug)
{
$ticket = tickets::whereSlug($slug)->firstOrFail();
$comments = $ticket->comments()->get;
return view('tickets.show',compact('ticket','comments'));
}
When exec this I receive the error:
ErrorException in TicketsController.php line 77: Undefined property: Illuminate\Database\Eloquent\Relations\HasMany::$get
What am I doing wrong?
Thanks.