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条回答
smile是对你的礼貌
2楼-- · 2020-02-08 04:38

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楼-- · 2020-02-08 04:39

Replace

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

by

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

in

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

查看更多
家丑人穷心不美
4楼-- · 2020-02-08 04:46

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
查看更多
登录 后发表回答