laravelista / comments

Native comments for your Laravel application.
MIT License
745 stars 144 forks source link

If i try to install i get ErrorException: Invalid argument supplied for foreach() #92

Closed KangarooMusiQue closed 4 years ago

KangarooMusiQue commented 4 years ago

If i try to install the package on Laravel Framework 7.7.1 via composer i get this error:


  Invalid argument supplied for foreach()

  at C:\Users\USERNAME\projekte\rolandradio2020\vendor\laravelista\comments\src\ServiceProvider.php:39
    35|      * Define permission defined in the config.
    36|      */
    37|     protected function definePermissions()
    38|     {
  > 39|         foreach(Config::get('comments.permissions') as $permission => $policy) {
    40|             Gate::define($permission, $policy);
    41|         }
    42|     }
    43|

  1   C:\Users\USERNAME\projekte\rolandradio2020\vendor\laravelista\comments\src\ServiceProvider.php:39
      Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Invalid argument supplied for foreach()", "C:\Users\USERNAME\projekte\rolandradio2
020\vendor\laravelista\comments\src\ServiceProvider.php", [])

  2   C:\Users\USERNAME\projekte\rolandradio2020\vendor\laravelista\comments\src\ServiceProvider.php:54
      Laravelista\Comments\ServiceProvider::definePermissions()
Script @php artisan package:discover --ansi handling the post-autoload-dump event returned with error code 1

Installation failed, reverting ./composer.json to its original content.
mabasic commented 4 years ago

I have just tried with a fresh installation of Laravel version 7.6.0 which uses Laravel Framework version 7.7.1 and it works...

Can't recreate.

dmitriydzyuba commented 4 years ago

Was experiencing same error during installation. Apparently it happens when you have your config cached. Config::get('comments.permissions') was returning NULL in my case. I commented out the code which was giving the error, installed the package successfully. Then I run php artisan config:cache and now everything is fine.

I think better to add an additional check in that method to avoid error.

KangarooMusiQue commented 4 years ago

I have tried to set a new "None"-Cache in the config/cache.php, but it did not helped to install. After clearing all caches before trying to install this package it worked. So if you get this error clearing all the caches right before installation helps. Thank you @dmitriydzyuba for pointing me to that.

mabasic commented 4 years ago

Was experiencing same error during installation. Apparently it happens when you have your config cached. Config::get('comments.permissions') was returning NULL in my case. I commented out the code which was giving the error, installed the package successfully. Then I run php artisan config:cache and now everything is fine.

I think better to add an additional check in that method to avoid error.

Aha, I see. Thank you for figuring it out. I will make a modification to this code Config::get('comments.permissions') to be Config::get('comments.permissions', []). This should work.

P.S. Before dealing with dependencies with Composer it is always best to clear all caches (config, route and view) and then apply cache after.

php artisan route:clear
php artisan config:clear
php artisan view:clear

composer require or composer install or composer update

php artisan config:cache
php artisan route:cache