0

I get an error when create a table seeder using model factory in laravel 8 but I don't know where I'm going wrong here.

This is an error:

Undefined constant "App\Models\boolean"

at C:\xampp\htdocs\mason\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\Factory.php:628

Here is my code:

Category.php:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Astrotomic\Translatable\Translatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Category extends Model
{
    use HasFactory, Translatable;

    protected $with = ['translations'];

    protected $translatedAttributes = ['name'];

    protected $hidden = ['translattions'];

    protected $casts = ['is_active' => boolean];

    protected $fillable = ['parent_id', 'slug', 'is_active'];
}

CategoryFactory.php:

<?php

namespace Database\Factories;

use App\Models\Category;
use Illuminate\Database\Eloquent\Factories\Factory;

class CategoryFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Category::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition(): array
    {
        return array(
            'name' => $this->faker->word(),
            'slug' => $this->faker->slug(),
            'is_active' => $this->faker->boolean(),
        );
    }
}

CategoryTableSeeder.php

<?php

namespace Database\Seeders;

use App\Models\Category;
use Illuminate\Database\Seeder;

class CategoryTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Category::factory(10)->create();
    }
}

1 Answers1

0

At Eloquent: Mutators & Casting we can read:

The $casts property should be an array where the key is the name of the attribute being cast and the value is the type you wish to cast the column to.

It doesn't mention having constants defined, though you can figure out given that one of the possible values listed is decimal:<digits>, which isn't a valid constant name.

Also, the example shown is:

protected $casts = [
    'is_admin' => 'boolean',
];
Álvaro González
  • 142,137
  • 41
  • 261
  • 360