Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ajagabos007's avatar

Seeder class Code review, refactory and best method to use

Please which is the best between method 1 (use Illuminate\Database\Eloquent\Factories\Sequence) and method 2 ( array and foreach ) to write my seeder class having known values?

<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\Category;
use Cviebrock\EloquentSluggable\Services\SlugService;
use Illuminate\Database\Eloquent\Factories\Sequence;

class CategorySeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // METHOD 1 : USING SEQUENCE 
        
        $categories = Category::factory()->count(9)->state(new sequence(
            [
                'name' => 'Home and Office',
                'slug' => SlugService::createSlug(Category::class, 'slug','Home and Office'),
            ],
            [
                'name' => 'Phone and Tablets',
                'slug' => SlugService::createSlug(Category::class, 'slug','Phone and Tablets'),
            ],
            [
                'name' => 'Computing',
                'slug' => SlugService::createSlug(Category::class, 'slug','Computing'),
            ],
            [
                'name' => 'Electronics',
                'slug' => SlugService::createSlug(Category::class, 'slug','Electronics'),
            ],
            [
                'name' => 'Fashion',
                'slug' => SlugService::createSlug(Category::class, 'slug','Fashion'),
            ],
            [
                'name' => 'Baby Products',
                'slug' => SlugService::createSlug(Category::class, 'slug','Baby Products'),
            ],
            [
                'name' => 'Gaming',
                'slug' => SlugService::createSlug(Category::class, 'slug','Gaming'),
            ],
            [
                'name' => 'Sporting Goods',
                'slug' => SlugService::createSlug(Category::class, 'slug','Sporting Goods'),
            ],
            [
                'name' => 'Automobile',
                'slug' => SlugService::createSlug(Category::class, 'slug','Automobile'),
            ],
            
        ));

        // METHOD 2: USING ARRAY AND FOREACH 
        /**
         * @var array $categories
         */
        $categories =[
            'Home and Office',
            'Phone and Tablets',
            'Computing',
            'Electronics',
            'Fashion',
            'Baby Products',
            'Gaming',
            'Sporting Goods',
            'Automobile',
        ];
        foreach($categories as $key=>$value){
            $category = new Category();
            $category->name = $value;
            $category->slug = SlugService::createSlug(Category::class, 'slug',$value);
            $category->save();
        }
    }

}

0 likes
8 replies
Sinnbeck's avatar

Why not the best of both ? :)

        $categories =[
            'Home and Office',
            'Phone and Tablets',
            'Computing',
            'Electronics',
            'Fashion',
            'Baby Products',
            'Gaming',
            'Sporting Goods',
            'Automobile',
        ];

        $categories = Category::factory()->count(9)->state(new sequence(collect($categories)->map(function($name) {
    return [
                'name' => $name
                'slug' => SlugService::createSlug(Category::class, 'slug', $name),
            ];
})->toArray()));
1 like
ajagabos007's avatar

@Sinnbeck wow...! I like this. it looks interesting. But how about the complexity of the code with the calling of additional functions ( the collect, map, and toArray )?

tykus's avatar
tykus
Best Answer
Level 104

@ajagabos007 it’s completely unnecessary IMHO; a seeder doesn’t have to use a Factory. You have a simple list of strings representing the names of Caregory instances; slug generation can be handled on the Model

$categories =[
            'Home and Office',
            'Phone and Tablets',
            'Computing',
            'Electronics',
            'Fashion',
            'Baby Products',
            'Gaming',
            'Sporting Goods',
            'Automobile',
 ];
foreach($categories as $category){
        Category::create([‘name’ =>$category]) ;
}
1 like
tykus's avatar

So the Factory is not creating any other properties ; it seems to add no value compared with a straight-up create method? I would keeps things simple and go with the second method.

Aside, why is it necessary to introduce the SlugService here; the Sluggable package should handle this (on creation) if the Category model is correctly setup.

1 like
ajagabos007's avatar

@tykus I am using Cviebrock\EloquentSluggable\Sluggable and I don't know if it will work but I will try it without the slug service Here is my category model setup based on the documentation

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;

class Category extends Model
{
    use HasFactory, sluggable;
    
    protected $fillable = [
        'name',
        'slug',
    ];

    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'name'
            ]
        ];
    }
    /**
     * Get all of the product's categories.
     */
    public function products(){
        return $this->morphedByMany(Product::class, 'categorable');
    }    
}

Please or to participate in this conversation.