首页 文章

count():参数必须是实现Countable的数组或对象

提问于
浏览
7

我面临着奇怪的情况 . 我在 生产环境 环境中遇到错误,而在开发环境中工作正常 .

开发:Laravel 5.4.28 PHP 7.0.13 MYSQL 5.7.17

制作:Laravel 5.4.28 PHP 7.2.1 MYSQL 5.7.20

在实现代码中 . 我用了:

namespace App;
use Illuminate\Support\Facades\Storage;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;

class Artwork extends Model
{
  use Searchable;

在开发中它工作正常 . 但是在 生产环境 中它给了我这个错误:count():参数必须是一个数组或在Builder.php中实现Countable的对象(第936行)

你可以在这张照片中看到:enter image description here任何想法背后的原因是什么?以及如何解决?

6 回答

  • 5

    这是documented change in PHP 7.2 . 您需要将Laravel更新为5.6或将PHP降级到7.1版 .

  • 1

    /将此代码放在您的路径文件的开头,它将正常工作/

    if(version_compare(PHP_VERSION, '7.2.0', '>=')) {
        error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
    }
    
  • 3

    我在Laravel 5.6中遇到了类似的问题 . 我在哪里得到基于对象的数组的错误 . 我知道该特定变量中的数据将始终保持对象,因此我习惯将对象转换为数组 . 这是代码示例: $objectData = (array)$objectData; echo "Total Elements in array are: ".count($objectData);

  • 1

    将以下行ob代码放在控制器中的类名之前

    if (version_compare(PHP_VERSION, '7.2.0', '>=')) {
    // Ignores notices and reports all other kinds... and warnings
    error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
    // error_reporting(E_ALL ^ E_WARNING); // Maybe this is enough
    }
    
  • -2

    我在Laravel 5.6中解决了这个问题

    //在控制器中

    public function index()
    {
    $todos = Todo::all();
    return view('todos.index')->with(['todos' => $todos]);
    
    }
    

    //在视图页面中

    @if(count($todos) > 0)
      @foreach($todos as $todo)
        <div class="well">
          <h3>{{$todo->text}}</h3>
          <span class="label label-danger">{{$todo->due}}</span>
        </div>
      @endforeach
    @endif
    
  • -4

    当我使用 Laravel 5.2PHP 7.2 时遇到同样的问题

    我更改了'vendor \ laravel \ framework \ src \ Illuminate \ Database \ Eloquent \ Builder.php'中的错误行1185:

    $originalWhereCount = is_array($query->wheres) ? count($query->wheres) : 0;

    这项工作对我而言

相关问题