For this example i assume you have a posts migration like the following
class CreatePostsTable extends Migration {
public function up() {
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId("user_id")->references("id")->on("users")->onDelete("cascade");
$table->string("name");
$table->timestamps();
});
}
public function down() {
Schema::dropIfExists('posts');
}
}
PostsFactory.php like this one
class PostFactory extends Factory {
protected $model = Post::class;
public function definition() {
return [
"name" => $this->faker->words($nb = 3, $asText = true)
];
}
}
and finally User.php model like this
class User extends Authenticatable {
use HasFactory, Notifiable;
public function posts() {
return $this->hasMany('App\Models\Post');
}
}
with that said you may do something like this in your DatabaseSeeder.php
User::factory()->times(3)->hasPosts(3)->create();
Then you can simply use:
"php artisan migrate --seed" if your database is empty
or "php artisan migrate:refresh --seed" in case it is not
Keep in mind that any records will be permanently lost after this action and the seeder will create new records