count(): Parameter must be an array or an object that implements Countable

╄→尐↘猪︶ㄣ 提交于 2019-12-17 22:37:14

问题


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:


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


来源:https://stackoverflow.com/questions/48343557/count-parameter-must-be-an-array-or-an-object-that-implements-countable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!