2

I am using laravel-comment to enable Users to comment on each other. Therefor, I need to use both the Commentable and the CanComment trait. But when I use them together, I get an error.

User uses it like this:

use Commentable, CanComment {
    Commentable::comments insteadof CanComment;
}

And I am trying to seed the comments like this:

foreach (User::all() as $user) {
    $receiver = User::where('id', '!=', $user->id)->inRandomOrder()->get();
    $user->comment($receiver, $faker->text(100), 3);
}

Even though the CanComment trait has a method called getCanBeRated, I get an error saying that it doesn't. Why is this happening?

Machavity
  • 30,841
  • 27
  • 92
  • 100
Fredrik
  • 3,027
  • 8
  • 33
  • 66

1 Answers1

5

You're getting this error because you're trying to use this method on collection and not on User object. Use first() instead of get() to get an object instead of collection:

$receiver = User::where('id', '!=', $user->id)->inRandomOrder()->first();
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279