그냥 사는 이야기

Laravel 5.3 Blog System - Setup Database 본문

Development/Web

Laravel 5.3 Blog System - Setup Database

없다캐라 2020. 1. 22. 10:49
반응형

Setup database

Auth 추가

php artisan make:auth
database/migrations/CreateUsersTable
$table->enum('role',['admin','author','subscriber'])->default('author');
php artisan migrate

database seed 추가

database/seeds/DatabaseSeeder
use App\User;

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        $this->call(UsersTableSeeder::class);
    }
}

class UsersTableSeeder extends Seeder
{
    public function run()
    {
        DB::table('users')->delete();

        User::create(array(
            'name' => 'admin',
            'email' => 'admin@example.com',
            'password' => bcrypt('111111'),
            'role' => 'admin',
        ));

        User::create(array(
            'name' => 'kmpak',
            'email' => 'kmpak@example.com',
            'password' => bcrypt('111111'),
            'role' => 'author',
        ));

         User::create(array(
            'name' => 'example',
            'email' => 'example@example.com',
            'password' => bcrypt('111111'),
            'role' => 'subscriber',
        ));
    }
}

post, comment 테이블 추가

php artisan make:migration create_posts_table

php artisan make:migration create_comments_table
database/migrations/CreatePostsTable
class CreatePostsTable extends Migration
{
  public function up()
  {
      // posts table
      Schema::create('posts', function(Blueprint $table) {
        $table->increments('id'); // table Id's
        $table -> integer('author_id')->unsigned()->default(0);
        $table->foreign('author_id')
              ->references('id')->on('users')
              ->onDelete('cascade');
        $table->string('title')->unique();
        $table->string('description'); // for meta description
        $table->text('body'); // our posts
        $table->string('slug')->unique();
        $table->string('images');
        $table->boolean('active');
        $table->timestamps();
      });
  }

    public function down()
    {
        Schema::drop('posts');
    }
}
database/migrations/CreateCommentsTable
class CreateCommentsTable extends Migration
{
    public function up()
    {
        // comments table
        Schema::create('comments', function(Blueprint $table) {
            $table->increments('id');
            $table -> integer('on_post')->unsigned()->default(0);
            $table->foreign('on_post')
                ->references('id')->on('posts')
                ->onDelete('cascade');
            $table -> integer('from_user')->unsigned()->default(0);
            $table->foreign('from_user')
                ->references('id')->on('users')
                ->onDelete('cascade');
            $table->text('body'); // our comments
            $table->timestamps();
        });
    }

    public function down()
    {
        // delete comments table
        Schema::drop('comments');
    }
}

Post, Comment Model & Seed 생성

php artisan make:model Post

php artisan make:model Comment
php artisan make:seeder PostsTableSeeder

php artisan make:seeder CommentsTableSeeder
database/seeds/PostsTableSeeder
use App\Post;
...
public function run()
{
    DB::table('posts')->delete();

    Post::create(array(
         'author_id' => 2,
         'title' => 'Hello World',
         'description' => 'My First Post',
         'body' => 'The most interesting function is show since the actual contents of the sequential file are generated here.

가장 재밌는 함수는 show다. 왜냐면 sequential file의 실제 내용이 만들어지기 때문이다.

For the sake of illustration, I present a slightly simplified version that abstracts some of the difficulties
associated with kprobes that would detract from the seq_file issues:

illustration 목적을 위해, 약간 간소화된 버전을 표현한다. kprobes와 연관된 어려운 것들 중 일부를 추상화 했다. seq_file 이슈에서 주의를 딴데로 돌리는 것.',
         'slug' => 'my-first-post',
         'images' => 'post-bg.jpg',
         'active' => '1',
     ));

     Post::create(array(
         'author_id' => 2,
         'title' => 'Extended Attributes and Access Control Lists',
         'description' => 'my next post',
         'body' => 'Many filesystems provide features that extend the standard functionality offered by the VFS layer.',
         'slug' => 'my-next-post',
         'images' => 'post-sample-image.jpg',
         'active' => '1',
     ));
}
database/seeds/CommentsTableSeeder
use App\Comment;
...
public function run()
{
    DB::table('comments')->delete();

    Comment::create(array(
         'on_post' => 1,
         'from_user' => 2,
         'body' => '우와아 직이네요',
     ));
}
database/seeds/DatabaseSeeder
...
public function run()
    {
        $this->call(UsersTableSeeder::class);
        $this->call(PostsTableSeeder::class);
        $this->call(CommentsTableSeeder::class);
    }

database refresh

php artisan migrate:refresh --seed
Comments