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

jaspercreel's avatar

Generating Factories and Seeders with AI

Anyone know of a good AI based solution for generating factories and seeders for a model that uses AI? Maybe something worth looking into to cut down the boilerplate.

0 likes
5 replies
LaryAI's avatar
Level 58

As of my last update, there isn't a widely-recognized AI tool specifically designed for generating factories and seeders for models in web development frameworks. However, you can leverage existing tools and techniques to reduce the boilerplate code when creating factories and seeders.

For Laravel, which is a common topic on Laracasts, you can use model factories and the Faker library to generate dummy data for your database. Here's a basic example of how you might create a factory and a seeder for a User model:

First, generate a factory for the User model:

php artisan make:factory UserFactory --model=User

Then, define the factory with the types of data you want to generate. For example:

// database/factories/UserFactory.php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Models\User;
use Faker\Generator as Faker;
use Illuminate\Support\Str;

$factory->define(User::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'email_verified_at' => now(),
        'password' => bcrypt('password'), // You should hash passwords
        'remember_token' => Str::random(10),
    ];
});

Next, create a seeder for the User model:

php artisan make:seeder UsersTableSeeder

In the seeder, use the factory to create a number of users:

// database/seeders/UsersTableSeeder.php

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

class UsersTableSeeder extends Seeder
{
    public function run()
    {
        // Create 50 users
        User::factory()->count(50)->create();
    }
}

Finally, call the seeder from the DatabaseSeeder:

// database/seeders/DatabaseSeeder.php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        $this->call(UsersTableSeeder::class);
    }
}

To run the seeder, execute:

php artisan db:seed

While this process doesn't eliminate all boilerplate, it's quite streamlined and doesn't require much beyond the initial setup. If you're looking for an even more automated solution, you might consider writing a custom command that introspects your models and generates factories and seeders based on the model attributes. However, this would require a fair amount of initial effort and might not be worth it unless you're setting up factories and seeders very frequently.

Tray2's avatar

I would suggest taking a look at Laravel Idea, it helps you generate all kinds of Laravel files, for example, when you create a model through Laravel Idea, you just check which other parts you want to create at the same time.

I just created a Book model, and through the Laravel Idea plugin I got this for free.

Model

<?php

namespace App\Models;

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

class Book extends Model
{
    use HasFactory;
}

Migration


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    public function up(): void
    {
        Schema::create('books', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('id');
            $table->string('title');
            $table->string('author');
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('books');
    }
};

FormRequest

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class BookRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'title' => ['required'],
            'author' => ['required'],
        ];
    }

    public function authorize(): bool
    {
        return true;
    }
}

Controller

<?php

namespace App\Http\Controllers;

use App\Http\Requests\BookRequest;
use App\Models\Book;

class BooksController extends Controller
{
    public function index()
    {
        return Book::all();
    }

    public function store(BookRequest $request)
    {
        return Book::create($request->validated());
    }

    public function show(Book $book)
    {
        return $book;
    }

    public function update(BookRequest $request, Book $book)
    {
        $book->update($request->validated());

        return $book;
    }

    public function destroy(Book $book)
    {
        $book->delete();

        return response()->json();
    }
}

ModelFactory

<?php

namespace Database\Factories;

use App\Models\Book;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;

class BookFactory extends Factory
{
    protected $model = Book::class;

    public function definition(): array
    {
        return [
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now(),
            'title' => $this->faker->word(),
            'author' => $this->faker->word(),
        ];
    }
}

The downside is that Laravel Idea costs a bit of money, but it's worth every penny.

3 likes
isslerman's avatar

Nice, but does not work with Visual Studio, is only for PHPStorm. :(

Please or to participate in this conversation.