Reputation: 3293
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?
Upvotes: 2
Views: 910
Reputation: 163768
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();
Upvotes: 5