【问题标题】:why im getting error count(): Parameter must be an array or an object that implements Countable in laravel?为什么我得到错误count():参数必须是在laravel中实现Countable的数组或对象?
【发布时间】:2020-03-07 20:18:50
【问题描述】:

我试图在我的项目中定义一个本地范围以在我的主页中显示流行(下载次数最多)...所以这是我的文件模型代码:

        public function scopePopular($query)
        {
        return $query->orderBy('file_download_num');
        }

如您所知,“file_download_num”是数据库中的字段,我试图从中获取数据... 所以这就是我在 HomeController 中使用这个范围的方式:

        $popularFiles = File::Popular()->get();

我没有在我的控制器或模型中使用count() 方法!但仍然出现错误!所以有什么建议吗? 注意:php版本为7.3

【问题讨论】:

  • 提供堆栈跟踪
  • @lagbox 我该怎么办?
  • @navid 希望对您有所帮助:stackoverflow.com/a/53425861/6850994
  • @Poldo 不幸的是没有工作......
  • 应该是File::popular()->get()

标签: laravel scope


【解决方案1】:

我发现在控制器中添加toArray() 方法可以帮助count() 方法正常工作...它为我工作...所以控制器应该是这样的:

$popularFiles = File::Popular()->get()->toArray();

【讨论】:

    猜你喜欢
    • 2019-05-10
    • 2020-08-15
    • 2020-04-27
    • 2018-09-22
    • 2020-03-02
    • 1970-01-01
    • 2019-02-26
    • 2019-06-20
    • 2020-04-08
    相关资源
    最近更新 更多