Skip to content

Demodata for category #1

@adrolli

Description

@adrolli

Add multilingual demodata to package moox/category:

// database/factories/CategoryFactory.php
namespace Moox\Category\Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use Moox\Category\Models\Category;
class CategoryFactory extends Factory
{
    protected $model = Category::class;
    public function definition(): array
    {
        return [
            // nicht-translatables
            'is_active' => true,
            'sort_order' => fake()->numberBetween(1, 1000),
        ];
    }
    public function withTranslations(array $locales): static
    {
        return $this->state(function () use ($locales) {
            $translations = [];
            foreach ($locales as $locale) {
                $faker = $this->fakerForLocale($locale);
                $title = $faker->unique()->words(2, true);
                $translations[$locale] = [
                    'title' => ucfirst($title),
                    'slug' => Str::slug($title),
                    'description' => $faker->paragraph(),
                ];
            }
            return $translations;
        });
    }
    protected function fakerForLocale(string $locale): \Faker\Generator
    {
        return fake(match ($locale) {
            'de' => 'de_DE',
            'en' => 'en_US',
            'fr' => 'fr_FR',
            'es' => 'es_ES',
            'it' => 'it_IT',
            'nl' => 'nl_NL',
            'pl' => 'pl_PL',
            default => 'en_US',
        });
    }
}

Seeder im Category-Package:

// database/seeders/CategoryDemoSeeder.php
namespace Moox\Category\Database\Seeders;
use Illuminate\Database\Seeder;
use Moox\Category\Models\Category;
class CategoryDemoSeeder extends Seeder
{
    public function run(): void
    {
        $locales = config('moox-demo.locales', ['de', 'en']);
        $count = config('moox-demo.category.count', 50);
        Category::factory()
            ->count($count)
            ->withTranslations($locales)
            ->create();
    }
}

Falls ihr mit moox/demo schon einen DemoContext habt:

public function run(DemoContext $context): void
{
    Category::factory()
        ->count($context->countFor('category', 50))
        ->withTranslations($context->locales)
        ->create();
}

Wichtig für Astrotomic:

// Category.php
use Astrotomic\Translatable\Translatable;
class Category extends Model
{
    use Translatable;
    public array $translatedAttributes = [
        'title',
        'slug',
        'description',
    ];
}

Translation Model:

class CategoryTranslation extends Model
{
    public $timestamps = false;
    protected $fillable = [
        'title',
        'slug',
        'description',
    ];
}

Dann erzeugt:

Category::factory()
    ->withTranslations(['de', 'en'])
    ->create();

automatisch:

categories
category_translations
de
en

Der Faker sitzt damit sauber im CategoryFactory des Category-Packages. moox/demo ruft später nur den CategoryDemoSeeder oder CategoryDemoProvider auf.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions