Feat: Add factories and basic seeder
This commit is contained in:
parent
d7886ae9b5
commit
d91b49275f
|
|
@ -11,10 +11,10 @@ class Article extends Model
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
||||||
/** The name of the database table storing this Model's records. */
|
/** The name of the database table storing this Model's records. */
|
||||||
protected string $table = 'articles';
|
protected $table = 'articles';
|
||||||
|
|
||||||
/** The attributes on the Model that can be mass-assigned */
|
/** The attributes on the Model that can be mass-assigned */
|
||||||
protected array $fillable = [
|
protected $fillable = [
|
||||||
'title',
|
'title',
|
||||||
'subtitle',
|
'subtitle',
|
||||||
'author_id',
|
'author_id',
|
||||||
|
|
|
||||||
|
|
@ -12,10 +12,10 @@ class Author extends Model
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
||||||
/** The name of the database table storing this Model's records. */
|
/** The name of the database table storing this Model's records. */
|
||||||
protected string $table = 'authors';
|
protected $table = 'authors';
|
||||||
|
|
||||||
/** The attributes on the Model that can be mass-assigned */
|
/** The attributes on the Model that can be mass-assigned */
|
||||||
protected array $fillable = [
|
protected $fillable = [
|
||||||
'display_name',
|
'display_name',
|
||||||
'profile_image_url',
|
'profile_image_url',
|
||||||
'user_id',
|
'user_id',
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,52 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use App\Models\Author;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Article>
|
||||||
|
*/
|
||||||
|
class ArticleFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The current User being used by the factory.
|
||||||
|
*/
|
||||||
|
protected static Author $author;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'title' => fake()->words(3, true),
|
||||||
|
'slug' => fake()->unique()->slug(),
|
||||||
|
'author_id' => static::$author->id ??= Author::factory()->create()->id,
|
||||||
|
'subtitle' => fake()->sentence(),
|
||||||
|
'body' => fake()->paragraphs(2, true),
|
||||||
|
'published_at' => fake()->date('Y-m-d H:m:s', now()->addDays(rand(0,20))),
|
||||||
|
'header_image_url' => fake()->imageUrl(),
|
||||||
|
'path_to_markdown_source' => fake()->name(),
|
||||||
|
'processed_at' => fake()->date('Y-m-d H:m:s'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicate the model's associated Author
|
||||||
|
*/
|
||||||
|
public function author(int|Author $author): static
|
||||||
|
{
|
||||||
|
if(is_int($author)) {
|
||||||
|
$author = Author::findOrFail($author);
|
||||||
|
}
|
||||||
|
static::$author = $author;
|
||||||
|
|
||||||
|
return $this->state(fn (array $attributes) => [
|
||||||
|
'author_id' => $author->id,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,50 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Auth\User;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Author>
|
||||||
|
*/
|
||||||
|
class AuthorFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The current User being used by the factory.
|
||||||
|
*/
|
||||||
|
protected static User $user;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'display_name' => fake()->userName(),
|
||||||
|
'profile_image_url' => fake()->imageUrl(),
|
||||||
|
'user_id' => static::$user->id ??= User::factory()->create()->id,
|
||||||
|
'biography' => fake()->paragraph(),
|
||||||
|
'primary_link' => fake()->url(),
|
||||||
|
'secondary_link' => rand(0,1) == 0 ? fake()->url() : null,
|
||||||
|
'mastodon_handle' => rand(0,1) == 0 ? fake()->userName() : null,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicate the model's associated User
|
||||||
|
*/
|
||||||
|
public function user(int|User $user): static
|
||||||
|
{
|
||||||
|
if(is_int($user)) {
|
||||||
|
$user = User::findOrFail($user);
|
||||||
|
}
|
||||||
|
static::$user = $user;
|
||||||
|
|
||||||
|
return $this->state(fn (array $attributes) => [
|
||||||
|
'user_id' => $user->id,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -41,4 +41,5 @@ class UserFactory extends Factory
|
||||||
'email_verified_at' => null,
|
'email_verified_at' => null,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,9 @@
|
||||||
|
|
||||||
namespace Database\Seeders;
|
namespace Database\Seeders;
|
||||||
|
|
||||||
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
use App\Models\User;
|
||||||
|
use App\Models\Author;
|
||||||
|
use App\Models\Article;
|
||||||
use Illuminate\Database\Seeder;
|
use Illuminate\Database\Seeder;
|
||||||
|
|
||||||
class DatabaseSeeder extends Seeder
|
class DatabaseSeeder extends Seeder
|
||||||
|
|
@ -12,11 +14,13 @@ class DatabaseSeeder extends Seeder
|
||||||
*/
|
*/
|
||||||
public function run(): void
|
public function run(): void
|
||||||
{
|
{
|
||||||
// \App\Models\User::factory(10)->create();
|
$test_user = User::factory()->create([
|
||||||
|
'name' => 'Test User',
|
||||||
|
'email' => 'test@example.com',
|
||||||
|
]);
|
||||||
|
|
||||||
// \App\Models\User::factory()->create([
|
$test_author = Author::factory()->user($test_user)->create();
|
||||||
// 'name' => 'Test User',
|
|
||||||
// 'email' => 'test@example.com',
|
Article::factory()->author($test_author)->count(30)->create();
|
||||||
// ]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue