mardi 19 mars 2019

Laravel 5: When Creating Model Factory of Enum Datatype Returns InvalidArgumentException: Data missing

I have a table rates for which I want to restrict the rate values from 1 to 5.

//  Migration file for rates table
    Schema::create('rates', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('user_id');
            $table->morphs('rated');
            $table->enum('rate', [1, 2, 3, 4, 5]);
            $table->timestamps();
        });

I have created a model factory for this table.

//RateFactory.php    
$factory->define(Rate::class, function (Faker $faker) {
    $chapter = factory(Chapter::class)->create();
    return [
        'user_id' => factory(User::class)->create()->id,
        'rate' => $faker->randomElement([1,2,3,4,5]),
        'rated_type'> get_class($chapter),
        'rated_id' => $chapter->id
    ];
});

When I use this factory in tinker:

$ php artisan tinker 

>>> factory(App\Rate::class)->create();

The terminal returns the exception below:

InvalidArgumentException with message 'Data missing'

According to the Laravel Columns Enum Documentation, I tried the same way.

Is enum not supported for model factory / testing ? Am I doing something wrong regarding the enums.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire