Skip to content

Commit

Permalink
Ep#28: Turbo Boost With Factories
Browse files Browse the repository at this point in the history
  • Loading branch information
arifktk32 committed Dec 24, 2021
1 parent 96a4c66 commit 643a949
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 45 deletions.
3 changes: 2 additions & 1 deletion app/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

class Category extends Model
{
protected $guarded = [];
use HasFactory;

protected $guarded = [];

public function posts() {
return $this->hasMany(Post::class);
}
Expand Down
21 changes: 21 additions & 0 deletions database/factories/CategoryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

class CategoryFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
"title" => $this->faker->word(),
"slug" => $this->faker->word()
];
}
}
26 changes: 26 additions & 0 deletions database/factories/PostFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Database\Factories;

use App\Models\Category;
use Illuminate\Database\Eloquent\Factories\Factory;

class PostFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
"user_id" => \App\Models\User::factory(),
"category_id" => Category::factory(),
"title" => $this->faker->sentence(),
"slug" => $this->faker->slug(),
"excerpt" => $this->faker->sentence(),
"body" => $this->faker->sentence()
];
}
}
48 changes: 4 additions & 44 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,12 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
Category::truncate();
User::truncate();
Post::truncate();

$user = \App\Models\User::factory()->create();

$personal = Category::create([
"title" => "Personal",
"slug" => "personal"
]);

$work = Category::create([
"title" => "Work",
"slug" => "work"
]);

$hobbies = Category::create([
"title" => "Hobbies",
"slug" => "hobbies"
]);

Post::create([
"user_id" => $user->id,
"category_id" => $personal->id,
"title" => "Just a personal post",
"slug" => "just-a-personal-post",
"excerpt" => "personal post excerpt",
"body" => "personal post body Lorem ipsum is simply a dummy text"
]);

Post::create([
"user_id" => $user->id,
"category_id" => $work->id,
"title" => "Post about my job work",
"slug" => "work-post",
"excerpt" => "work post excerpt",
"body" => "work post body Lorem ipsum is simply a dummy text"
$user = \App\Models\User::factory()->create([
'name' => "John Doe"
]);

Post::create([
"user_id" => $user->id,
"category_id" => $hobbies->id,
"title" => "Post about my hobbies",
"slug" => "hobbies-post",
"excerpt" => "hobbies post excerpt",
"body" => "hobbies post body Lorem ipsum is simply a dummy text"
Post::factory(5)->create([
"user_id" => $user->id
]);
}
}

0 comments on commit 643a949

Please sign in to comment.