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

2020-02-08 03:57发布

问题:

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:

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

回答1:

/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);
}


回答2:

This is a documented change in PHP 7.2. You need to either update Laravel to 5.6 or downgrade PHP to version 7.1.



回答3:

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);



回答4:

Replace

$originalWhereCount = count($query->wheres);

by

$originalWhereCount = count((array)$query->wheres);

in

\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php



回答5:

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
}


回答6:

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', ...];
}


回答7:

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

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


回答8:

Model looking for countable parameter:

class ClassName extend Model {
    protected $fillable=['column_name']; // column in DB of Model is in array
}


回答9:

I Solve this in Laravel 5.6

// in controller

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

}

// in view page

@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