thedevdojo / voyager

Voyager - The Missing Laravel Admin
https://voyager.devdojo.com
MIT License
11.78k stars 2.67k forks source link

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

Closed ghost closed 5 years ago

ghost commented 5 years ago

Version information

Description

ErrorException (E_ERROR) count(): Parameter must be an array or an object that implements Countable (View: resources/views/vendor/voyager/events/browse.blade.php)

Steps To Reproduce

Steps to reproduce the behavior:

  1. Just create a new BREAD Events
  2. Then when trying to create first event
  3. Then on the Event List page, it shows this error
  4. The error is somewhere in this line, in the

File- project/vendor/tcg/voyager/resources/views/bread/browse.blade.php Line number - 112 Line - @if (count(json_decode($data->{$row->field})) > 0)

emptynick commented 5 years ago

(View: resources/views/vendor/voyager/events/browse.blade.php)

And

File- project/vendor/tcg/voyager/resources/views/bread/browse.blade.php

Is not the same. Thats your custom view.

ghost commented 5 years ago

Hello,

I just made a custom view by copying the code from the project/vendor/tcg/voyager/resources/views/bread/browse.blade.php

And by the way in my custom file, I just fixed the issue by doing- from this- @if (count(json_decode($data->{$row->field})) > 0)

to this- @if (!empty(json_decode($data->{$row->field})))

Because in PHP 7.* versions, if we pass an empty array to count(), then it returns an error.

Is not the same. That's your custom view.

emptynick commented 5 years ago

Are you able to create a PR for this?

stefanos82 commented 5 years ago

@classiebit I remember this silly thing from PHPMyAdmin.

Your original code is

@if (count(json_decode($data->{$row->field})) > 0)

Can you try wrapping your count inside parentheses?

@if ((count(json_decode($data->{$row->field}))) > 0)

This should resolve the issue.

suhaboncukcu commented 5 years ago

The problem is that with PHP7.X versions only entities implementing Countable interface can be used as a countable and count() function is only able to use countables. So while count(json_decode('{}', true)) is working as expected, count(json_decode('{}')) will throw an error. (check this: https://3v4l.org/4JF9V)

But anyway, I guess checking the condition with empty would be more performant than counting the vals and checking the result with > 0

I can open a detailed PR if nobody is picking this up.

stefanos82 commented 5 years ago

In the http://php.net/manual/en/function.json-decode.php explains it clearly why the use of true make it work as expected:

image

suhaboncukcu commented 5 years ago

In the http://php.net/manual/en/function.json-decode.php explains it clearly why the use of true make it work as expected:

image

that's why I said that :) arrays, even when they're empty, are countable.

stefanos82 commented 5 years ago

Yes, but json_decode without the explicit use of the second argument, it defaults to false, therefore it's not converted to associative array.

emptynick commented 5 years ago

Fixed in https://github.com/the-control-group/voyager/pull/3909 commit https://github.com/the-control-group/voyager/pull/3909/commits/469dad1b1616bfcde0111ef9300d6a746653adbc

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.