0

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.

1 Answer 1

2

It seems you're trying to access the get property when i think your purpose is to call the get() method. Replace this:

$comments = $ticket->comments()->get;

with

$comments = $ticket->comments()->get();
Sign up to request clarification or add additional context in comments.

1 Comment

Woow nice! Seriously looked hours to this problem!! thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.