【问题标题】:PHP Traits "Method does not exist"PHP 特征“方法不存在”
【发布时间】:2018-06-20 02:53:17
【问题描述】:

我正在使用laravel-comment 使用户能够相互评论。因此,我需要同时使用 CommentableCanComment 特征。但是当我将它们一起使用时,我得到了一个错误。

用户这样使用它:

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

我正在尝试像这样播种 cmets:

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

尽管 CanComment 特征有一个名为 getCanBeRated 的方法,但我收到一条错误消息说它没有。为什么会这样?

【问题讨论】:

    标签: php laravel traits


    【解决方案1】:

    您收到此错误是因为您尝试在收集而不是User 对象上使用此方法。使用first() 代替get()get an object instead of collection

    $receiver = User::where('id', '!=', $user->id)->inRandomOrder()->first();
    

    【讨论】:

      猜你喜欢
      • 2013-12-21
      • 2014-06-29
      • 1970-01-01
      • 2021-01-17
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-11
      相关资源
      最近更新 更多