You can use Factories to quickly scaffold example data. Meaning that you can very easily create a few dozen to hundreds or even thousands of rows / entries of example data in a database table.
Create a Factory
Here’s how you can create a factory (command prompt):
php artisan make:factory JobFactoryThis will create a “PostFactory” Factory class in: database\factories
This is how it will look like in your code editor:
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Job>
*/
class JobFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
// add the columns you have in your db. For example:
// 'title' => fake()->jobTitle(),
// 'salary' => fake()->numberBetween(50000, 150000),
];
}
}
Call a Factory
To call this factory, use the following command in your command prompt in a tinker session:
App\Models\Job::factory()->create();Now this create a new row with a random job name and a random salary between 50,000 and 150,000.
If you want to create 100 rows you can just add the amount like here:
App\Models\Job::factory(100)->create();Now it will create 100 rows with random job names and salary in milliseconds / seconds.
Override Values
Now mainly for testing purposes you might want to change certain values. There’s also an easy way to do that, but lets switch to a user factory for a easier example.
Let’s say this is your basic factory function:
public function definition(): array
{
return [
'first_name' => fake()->firstName(),
'last_name' => fake()->lastName(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),
'remember_token' => Str::random(10),
'admin' => false
];
}Now as mentioned before for tests you might want to make a user a “admin” or make him unverified. Here’s how:
public function unverified(): static
{
return $this->state(fn (array $attributes) => [
'email_verified_at' => null,
]);
}
public function admin(): static
{
return $this->state(fn (array $attributes) => [
'admin' => true,
]);
}To call these functions its a bit different but still simple. Here’s how (again in the command prompt in a tinker session):
App\Models\user::factory()->unverified()->create(); // to make a unverified user
App\Models\user::factory()->admin()->create(); // to create a admin userHow they work is they take the values given in the “definition()” function and then overrides the values with the ones in the given (in this case) “unverified()” / “admin()” functions.
Assign a Foreign Key (Relationships)
When working with database’s you will most probably use or have used or at least heard of Primary & Foreign Keys. To add a Foreign Key in a Factory do the following:
Let’s say you have a table called “jobs_listing” with the class in PHP called Job and a table called “employer” with the class in PHP called “Employer”. And you want the Foreign Key in the “jobs_listing” table.
In the “jobs_listing” migration file, add either of these both work the same:
public function up(): void
{
Schema::create('job_listings', function (Blueprint $table) {
// $table->id();
$table->unsignedBigInteger('employer_id');
// either the one above or below
$table->foreignIdFor(\App\Models\Employer::class);
// $table->string('title');
// $table->string('salary');
// $table->timestamps();
});
}And if your wondering why “BigInteger” its because that’s what an id is its a big integer that automatically increments.
Now since you’ll probably also want this in your factory add this in the “JobFactory” file:
public function definition(): array
{
return [
// 'title' => fake()->jobTitle(),
'employer_id' => Employer::factory(),
// 'salary' => '$' . fake()->numberBetween(50000, 150000),
];
}