17. Post create

yeomc·2021년 7월 16일
1

라라벨8 레딧

목록 보기
17/22

Post 모델 생성

php artisan make:model Post -m

생성된 migration 파일 수정

Schema::create('posts', function (Blueprint $table) {
  $table->id();
  $table->foreignId('community_id');
  $table->foreignId('user_id');
  $table->string('title');
  $table->text('post_text')->nullable();
  $table->text('post_link')->nullable();
  $table->timestamps();
  $table->softDeletes();
});

migrate

php artisan migrate

Post 모델 수정

use HasFactory, SoftDeletes;

protected $fillable = ['community_id', 'user_id', 'title', 'post_text', 'post_link'];

public function community()
{
  return $this->belongsTo(Community::class);
}

public function user()
{
  return $this->belongsTo(User::class);
}

Community 모델 수정

public function posts()
{
  return $this->hasMany(Post::class);
}

Post 컨트롤러 생성

php artisan make:controller CommunityPostController --resource

Post 컨트롤러 수정

public function index(Community $community)
{
  return redirect()->route('communities.show', $community);
}

public function create(Community $community)
{
  return view("posts.create", compact('community'));
}

routes 추가

Route::resource('communities.posts', CommunityPostController::class);

community show view 수정

// post create 링크.
<a href="{{ route('communities.posts.create', $community) }}" class="button is-info is-rounded is-fullwidth has-text-weight-bold">
  {{ __('Add a Post') }}
</a>
                                        
<a href="{{ route('communities.posts.create', $community) }}"  class="button is-info is-rounded is-fullwidth has-text-weight-bold">
  {{ __('Create Post') }}
</a>

create view 생성

// posts/create.blade.php
@extends('layouts.app')

@section('content')
    <section class="section hero is-fullheight-with-navbar">
        <div class="hero-body">
            <div class="container">
                <div class="columns is-centered">
                    <div class="column is-8">
                        @component('components.card')
                            @slot('title')
                                {{ $community->name }} - {{ __('Create a post') }}
                            @endslot

                            <form method="POST" action="{{ route('communities.posts.store', $community) }}" enctype="multipart/form-data">
                                @csrf

                                <div class="field">
                                    <label class="label" for="post_text">{{ __('Title') }}</label>
                                    <div class="control">
                                        <input id="title" type="text" class="input @error('title') is-danger @enderror"
                                            name="title" value="{{ old('title') }}" placeholder="Title" required autofocus>
                                    </div>

                                    @error('title')
                                        <p class="help is-danger" role="alert">
                                            {{ $message }}
                                        </p>
                                    @enderror
                                </div>

                                <div class="field">
                                    <label class="label" for="post_text">{{ __('Text') }}</label>
                                    <div class="control">
                                        <textarea id="post_text" name="post_text"
                                            class="textarea @error('post_text') is-danger @enderror"
                                            placeholder="Text (optional)">{{ old('post_text') }}</textarea>
                                    </div>

                                    @error('post_text')
                                        <p class="help is-danger" role="alert">
                                            {{ $message }}
                                        </p>
                                    @enderror
                                </div>

                                <div class="field">
                                    <label class="label" for="post_image">{{ __('Image') }}</label>
                                    <div class="control">
                                        <div class="file">
                                            <label class="file-label">
                                                <input class="file-input" type="file" name="post_image">
                                                <span class="file-cta">
                                                    <span class="file-icon">
                                                        <i class="fas fa-upload"></i>
                                                    </span>
                                                    <span class="file-label">
                                                        Upload
                                                    </span>
                                                </span>
                                            </label>
                                        </div>
                                    </div>

                                    @error('post_image')
                                        <p class="help is-danger" role="alert">
                                            {{ $message }}
                                        </p>
                                    @enderror
                                </div>

                                <div class="field">
                                    <label class="label" for="post_link">{{ __('Link') }}</label>
                                    <div class="control">
                                        <input id="post_link" type="text" class="input @error('post_link') is-danger @enderror"
                                            name="post_link" value="{{ old('post_link') }}" placeholder="Url">
                                    </div>

                                    @error('post_link')
                                        <p class="help is-danger" role="alert">
                                            {{ $message }}
                                        </p>
                                    @enderror
                                </div>

                                <hr>

                                <div class="field is-grouped is-grouped-right">
                                    <p class="control">
                                        <a class="button is-rounded" href="{{ route('communities.show', $community) }}">
                                            <strong>{{ __('Cancel') }}</strong>
                                        </a>
                                    </p>
                                    <p class="control">
                                        <button type="submit" class="button is-info is-rounded">
                                            <strong>{{ __('Post') }}</strong>
                                        </button>
                                    </p>
                                </div>
                            </form>
                        @endcomponent
                    </div>
                </div>
            </div>
        </div>
    </section>
@endsection

git commit

git add .
git commit -m "feat: create post create view"
profile
염씨 클론 코딩

0개의 댓글