count(): Parameter must be an array or an object t

2020-02-08 04:10发布

I'm facing strange case. I face an error in production env not while in dev it's working fine.

Development: Laravel 5.4.28 PHP 7.0.13 MYSQL 5.7.17

Production: Laravel 5.4.28 PHP 7.2.1 MYSQL 5.7.20

In implementation code. I used:

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

class Artwork extends Model
{
  use Searchable;

In development it works fine. But in production it gives me this error: count(): Parameter must be an array or an object that implements Countable in Builder.php (line 936)

as you can see in this pic:

enter image description here

Any idea what is the reason behind this? and how to fix?

9条回答
成全新的幸福
2楼-- · 2020-02-08 04:28

/Put this code at the beginning your routes file its will work fine/

if(version_compare(PHP_VERSION, '7.2.0', '>=')) {
    error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
}
查看更多
姐就是有狂的资本
3楼-- · 2020-02-08 04:28

place the below line ob code before the class name in your controllers

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
}
查看更多
老娘就宠你
4楼-- · 2020-02-08 04:29

I was facing the same issue with an external created table (Not using migration or command), After creating the model, I just assigned a table name, but the problem was in my model protected $fillable where I assign string instead of array and error occurred. There is 2 possible solution for that.

  1. Assign an array to your protected $fillable = ['filed1', 'filed2'];
  2. Remove protected $fillable completely (Not Recommended)
class Abc extends Model
{
     protected  $table = 'cities';
     protected $fillable = ['field1','field2', ...];
}
查看更多
该账号已被封号
5楼-- · 2020-02-08 04:33

Model looking for countable parameter:

class ClassName extend Model {
    protected $fillable=['column_name']; // column in DB of Model is in array
}
查看更多
不美不萌又怎样
6楼-- · 2020-02-08 04:36

I was facing similar issue in Laravel 5.6. Where I was getting error for object based array. I knew that data in that particular variable will always remain object so i used to convert the object to array. Here is code sample: $objectData = (array)$objectData; echo "Total Elements in array are: ".count($objectData);

查看更多
做自己的国王
7楼-- · 2020-02-08 04:36

'vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php' to:

$originalWhereCount = is_array($query->wheres) ? count($query->wheres) : 0;
查看更多
登录 后发表回答