데이터가 많이 필요하다.

factory 기능을 사용해서 만들어보자

 

[명령어]

php artisan make:factory

 

[결과물]

<?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()
    {
        return [
            //
        ];
    }
}

 

[작성]

db에 맞게 필드값과

내가원하는 자료 들을 설정할수있다

<?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()
    {
         return [
            'title' => $this->faker->unique()->sentence(),
            'excerpt' => $this->faker->realText($maxNbChars = 50),
            'body' => $this->faker->text(),
            'image_path' => $this->faker->imageUrl(640, 480),
            'is_published' => 1,
            'min_to_read' => $this->faker->numberBetween(1, 10)
        ];
    }
}

 

 

seed 명령어를 치면 dataseeder 부터 동작하기 떄문에

코드를 더해준다.

Post 모델이 없다면 추가 해준다 명령어 : php artisan make:model Post

<?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.
     *
     * @return void
     */
    public function run()
    {
        
        Post::factory(100)->create();
    }
}

 

작성후 

[명령어]

php artisan db:seed

를 넣으면 데이터가 100개 추가된다.

 

 

혹시 중간에 데이터가 맘에들지않아 오버라이딩을 해줄수도있다.

 

<?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.
     *
     * @return void
     */
    public function run()
    {
        // \App\Models\User::factory(10)->create();

        // \App\Models\User::factory()->create([
        //     'name' => 'Test User',
        //     'email' => 'test@example.com',
        // ]);
        //$this->call(PostTableseeder::class);
        Post::factory(100)->create(
            [
                'body'=>'overriding body'
            ]

        );
    }
}
반응형

라라벨은 migration 을 사용하여서 db를 생성,편집,삭제 등을 합니다

sql 의 쿼리문을 php 함수형으로 볼수있습니다

 

기본적인 creat table 을 해보겠습니다

 

php artisan make:migration create_post_table

[결과물]

<?php

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

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

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
};

 

명령어에 내가 만들고 싶은 table 이름을

php artisan make:migration create_{원하는이름}_table

이렇게 만들어준다.

 

그 이후 내가 원하는 필드를 만드는 코드를 작성한다.

<?php

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

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

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
};

 

그이후 마이그래이트 명령어를한다

php artisan migrate

[결과물]

위에 있는 코드와 동일하게 만들어진다.

반응형

+ Recent posts