Skip to content

Instantly share code, notes, and snippets.

@alqahtani
Created December 12, 2020 20:05
Show Gist options
  • Select an option

  • Save alqahtani/d64840b3f7744e0e6c069dab9ed70013 to your computer and use it in GitHub Desktop.

Select an option

Save alqahtani/d64840b3f7744e0e6c069dab9ed70013 to your computer and use it in GitHub Desktop.
Following with build forum with TDD on Laracasts
<?php
namespace Database\Factories;
use App\Models\User;
use App\Models\Reply;
use App\Models\Thread;
use Illuminate\Database\Eloquent\Factories\Factory;
class ReplyFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Reply::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'user_id' => User::factory(),
'thread_id' => Thread::factory(),
'body' => $this->faker->paragraph()
];
}
}
<?php
namespace Database\Factories;
use App\Models\User;
use App\Models\Thread;
use Illuminate\Database\Eloquent\Factories\Factory;
class ThreadFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Thread::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'user_id' => User::factory(),
'title' => $this->faker->sentence(),
'body' => $this->faker->paragraph()
];
}
}
<?php
// first command to create threads
$threads = App\Models\Thread::factory()->count(50)->create();
// second command to create replies for the created threads
$threads->each(function($thread) { App\Models\Reply::factory()->count(10)->create(['thread_id'=> $thread->id]); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment