Les comparto el paso a paso:
Crear proyecto
laravel new api-8
Entrar a la carpeta y crear Model-Controller-Migration-Factory
cd api-8
php artisan make:model Post -cmf
migrations/…post
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('title');
$table->string('slug')->unique();
$table->text('content');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
PostFactory.php
public function definition()
{
return [
'user_id' => rand(1,10),
'title' => $this->faker->sentence,
'slug' => $this->faker->slug,
'content' => $this->faker->text(1600)
];
}
DatabaseSeeder.php
public function run()
{
\App\Models\User::factory(10)->create();
\App\Models\Post::factory(120)->create();
}
Comando de migración:
php artisan migrate --seed
¿Quieres ver más aportes, preguntas y respuestas de la comunidad?