Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File Cache driver issue introduced in 5.5 when running seeds #21903

Closed
TrieBr opened this issue Nov 1, 2017 · 3 comments
Closed

File Cache driver issue introduced in 5.5 when running seeds #21903

TrieBr opened this issue Nov 1, 2017 · 3 comments

Comments

@TrieBr
Copy link

TrieBr commented Nov 1, 2017

  • Laravel Version: 5.5.19
  • PHP Version: 7.0.22
  • Database Driver & Version: MySQL 5.7.20

Description:

When the file cache driver is selected, I sporadically get the following error when running db:seed or migrate:refresh --seed:

error:Call to undefined method App\Role::firstOrCreate()

I'm quite certain this was introduced in Laravel 5.5. I've been using the same Role Seeder class on several Laravel 5.4 projects, and I first noticed it when I created a Laravel 5.5 project several weeks back.

See my Stack Overflow question for more details on the problem. After several hours, I realized after running cache:clear several times seemed to make the seeder run successfully sometimes. Then I realized switching the cache driver to redis resolved the issue completely.

Steps To Reproduce:

I found the original problem on Linux with the version information specified above, however I created the following Steps using PHP 7.1.10 on Windows 64 bit.

  1. composer create-project --prefer-dist laravel/laravel laravel-bug
  2. Follow Entrust Installation and Configuration steps.
  3. When running the command: php artisan entrust:migration, you will get the following error:

Method Zizaco\Entrust\MigrationCommand::handle() does not exist

  1. Edit vendor\zizaco\entrust\src\commands\MigrationCommand.php and change public function fire() to public function handle()
  2. Run php artisan entrust:migration again.
  3. Create a seeder: php artisan make:seeder RoleSeeder
  4. Edit database/seeds/RoleSeeder.php to be the following:
<?php

use Illuminate\Database\Seeder;
use App\Permission;

class RoleSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {


        //Application User: All users have this role be default
        $r = \App\Role::firstOrCreate(['name'=>'app-user'],
            [
                'name'         => 'app-user',
                'display_name' => 'All users of the application',
                'description'  => 'Uses the application',
            ]
        );



    }
}
  1. run php artisan migrate
  2. run php artisan db:seed --class=RoleSeeder
  3. An error will be printed. If you do not see the error, try running the above commands a couple times:
  [BadMethodCallException]
  Call to undefined method App\Role::firstOrCreate()
  1. In .env file, set CACHE_DRIVER=redis
  2. run php artisan db:seed --class=RoleSeeder. The error will not appear.

I put code that demonstrates the problem here: https://github.com/TrieBr/laravel-bug
You should be able to just pull that code and then perform steps 8 through 12 above to demonstrate.

@sisve
Copy link
Contributor

sisve commented Nov 1, 2017

My first thought; does this occur on your own models? Entrust does not, like you've described, support Laravel 5.5 yet.

@TrieBr
Copy link
Author

TrieBr commented Nov 2, 2017

I was able to isolate the problem. Entrust is not the problem.

If I change the Role class to this:

<?php namespace App;

use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Config;



class Role extends Model
{
        public function save(array $options = [])
    {   //both inserts and updates
        $result = parent::save($options);
        Cache::tags(Config::get('entrust.permission_role_table'))->flush();
        return $result;
    }
}

Since the file cache store does not handle tagging, the following exception is thrown:

  [BadMethodCallException]
  This cache store does not support tagging.

However, for some reason, the Exception handling system is catching the BadMethodCallException and and printing the wildly incorrect/misleading message:

  [BadMethodCallException]
  Call to undefined method App\Role::firstOrCreate()

As I mentioned, this is seems definitely new to Laravel 5.5, because I've seen the This cache store does not support tagging exception being reported many times when working with Laravel 5.4/Entrust.

@themsaid
Copy link
Member

themsaid commented Nov 2, 2017

Handled in #21929

Thanks

@themsaid themsaid closed this as completed Nov 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants