spatie / laravel-enum

Laravel support for spatie/enum
https://spatie.be/open-source
MIT License
344 stars 37 forks source link

How to use FakerEnumProvider? #96

Closed francoism90 closed 2 years ago

francoism90 commented 2 years ago

I would like to register this provider in AppServiceProvider:

$this->app->singleton(Faker::class, function ($app) {
            $faker = new Faker();
            $faker->addProvider(new FakerEnumProvider($faker));

            return $faker;
        });

Result: Unknown format "text" It seems it cannot find the default Faker factories/methods, any tips?

Thanks!

francoism90 commented 2 years ago

To answer my own question:

<?php

namespace App\Providers;

use Faker\Factory;
use Faker\Generator;
use Illuminate\Support\ServiceProvider;
use Spatie\Enum\Laravel\Faker\FakerEnumProvider;
use Illuminate\Database\Eloquent\Factories\Factory as Factories;

class FakerServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton(Generator::class, function ($app) {
            $faker = Factory::create();
            $faker->addProvider(new FakerEnumProvider($faker));
            return $faker;
        });
    }

    public function boot()
    {
        Factories::guessFactoryNamesUsing(function (string $modelName) {
            return 'Database\\Factories\\'.class_basename($modelName).'Factory';
        });
    }
}
francoism90 commented 2 years ago

Hmm, still:

Call to undefined method Database\Factories\PostFactory::randomEnum()
francoism90 commented 2 years ago

Yep.. I'm not awake yet:

'status' => $this->faker->randomEnum(Status::class),

I did:

'status' => $this->randomEnum(Status::class),

Sorry for the noise.