Create fresh project laravel 10 dan membuat model

use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema;

return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('body'); $table->timestamps(); }); }

<em>/**</em>

* Reverse the migrations. */ public function down(): void { Schema::dropIfExists('posts'); } };Replace <strong>app/Models/Post.php menjadi seperti berikut:<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model;

class Post extends Model { use HasFactory;

protected $fillabel = [
'title',
'body'
];

}Replace <strong>database/factories/PostFactory.php  menjadi seperti berikut:<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/ * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Post> */ class PostFactory extends Factory { / * Define the model's default state. * * @return array<string, mixed> */ public function definition(): array { return [ 'title' => $this->faker->sentence, 'body' => $this->faker->paragraph ]; } }Kemudian replace database/seeders/DatabaseSeeder.php<?php

namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;

use App\Models\Post; use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder { /** * Seed the application's database. */ public function run(): void { // membuat satu row di table users \App\Models\User::factory()->create([ 'name' => 'admin', 'email' => 'admin@admin.com', ]);

<em>// membuat 5 row data dummy di table posts</em>
Post::factory(5)-&gt;create();
}

}jika sudah mereplace file di atas , jalankan perintah berikut:sebelum menjalakan perintah di bawah ini anda sudah membuat database baruphp artisan migrate:fresh --seed

Komentar

Ada 0 komentar pada episode ini.