diff --git a/app/Models/Article.php b/app/Models/Article.php index 928563b..001efc6 100644 --- a/app/Models/Article.php +++ b/app/Models/Article.php @@ -11,10 +11,10 @@ class Article extends Model use HasFactory; /** 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 */ - protected array $fillable = [ + protected $fillable = [ 'title', 'subtitle', 'author_id', diff --git a/app/Models/Author.php b/app/Models/Author.php index a7971b0..78570f8 100644 --- a/app/Models/Author.php +++ b/app/Models/Author.php @@ -12,10 +12,10 @@ class Author extends Model use HasFactory; /** 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 */ - protected array $fillable = [ + protected $fillable = [ 'display_name', 'profile_image_url', 'user_id', diff --git a/database/factories/ArticleFactory.php b/database/factories/ArticleFactory.php new file mode 100644 index 0000000..232de60 --- /dev/null +++ b/database/factories/ArticleFactory.php @@ -0,0 +1,52 @@ + + */ +class ArticleFactory extends Factory +{ + /** + * The current User being used by the factory. + */ + protected static Author $author; + + /** + * Define the model's default state. + * + * @return array + */ + 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, + ]); + } +} diff --git a/database/factories/AuthorFactory.php b/database/factories/AuthorFactory.php new file mode 100644 index 0000000..d52c10c --- /dev/null +++ b/database/factories/AuthorFactory.php @@ -0,0 +1,50 @@ + + */ +class AuthorFactory extends Factory +{ + /** + * The current User being used by the factory. + */ + protected static User $user; + + /** + * Define the model's default state. + * + * @return array + */ + 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, + ]); + } +} diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 584104c..03435d3 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -41,4 +41,5 @@ class UserFactory extends Factory 'email_verified_at' => null, ]); } + } diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index a9f4519..c148c63 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -2,7 +2,9 @@ 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; class DatabaseSeeder extends Seeder @@ -12,11 +14,13 @@ class DatabaseSeeder extends Seeder */ 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([ - // 'name' => 'Test User', - // 'email' => 'test@example.com', - // ]); + $test_author = Author::factory()->user($test_user)->create(); + + Article::factory()->author($test_author)->count(30)->create(); } }