速習Larave3(その13)、コメントNo.2

タグ: Laravel3  

前回に引き続き、コメントの投稿システムを追加して行きましょう。

前記事:速習Larave3(その13)、コメントNo.1

今回は、まずコードを先出しし、後で解説していきます。全部関連しているからです。

Postコントローラーの変更

記事の表示でコメントと入力フォームを表示します。そこで、コメントもビューに渡してあげる必要がでました。

    /**
     * 記事表示
     */
    public function get_show($id)
    {
        $post = Post::with(array('comments', 'comments.user'))->find($id);

        if ( $post === null )
        {
            return Redirect::error('404');
        }

        return View::make('post.show')
                ->with('post', $post);
    }

シンプルですね。解説は後ほど。

コメントコントローラー

ルート定義を注意深く読まれた方は、予想していたでしょう。Commentコントローラーを追加します。application/controlles/comment.phpです。

<?php

class Comment_Controller extends Base_Controller
{
    public $restful = true;

    public function post_add($id)
    {
        $post = Post::find($id);

        if ( $post === null )
        {
            return Redirect::error('404');
        }

        $rules = array(
            'comment' => 'required|max:2000',
        );

        $inputs = Input::only('comment');

        $val = Validator::make($inputs, $rules);

        if ( $val->passes() )
        {
//            $comment = new Comment(array(
//                'user_id' => Auth::user()->id,
//                'post_id' => $id,
//                'comment' => $inputs['comment'],
//            ));
//            $comment->save();
            $comment = new Comment(array(
                'user_id' => Auth::user()->id,
                'comment' => $inputs['comment'],
            ));
            $post->comments()->insert($comment);

            return Redirect::to_route('post-show', array($id))
                ->with('message', 'コメントを投稿しました。');
        }
        else
        {
            return Redirect::to_route('post-show', array($id))
                ->with_input()
                ->with_errors($val);
        }
    }

}

記事表示ビューの変更

繰り返しますが、コメントとコメント入力フォームは記事の表示ページで一緒に表示します。そのためビューフォルダーのpostサブフォルダーの中にあるshow.blade.phpを以下のように変更してください。

@layout('template')

@section('title')
    ポスト表示
@endsection

@section('content')
    <h2>{{ e($post->title) }}</h2>
    <div>{{ e($post->body) }}</div>
    {{-- コメント表示 --}}
    <h5>コメント</h5>
    @forelse ( $post->comments as $comment )
        <div>
            {{ $comment->comment }} {{ $comment->user->username }}さん ({{ $comment->created_at }})
        </div>
    @empty
        <p>コメントはまだありません</p>
    @endforelse
    {{-- コメント入力フォーム --}}
    @if ( Role::check_register() )
        {{ Form::open(route('comment-add', $post->id)) }}
            <div>
                {{ Form::label('comment', 'コメント') }}<br>
                {{ Form::textarea('comment', Input::old('comment', '')) }}
            </div>
            @if ( $errors->has('comment') )
                <p style="color: red;">{{ $errors->first('comment') }}</p>
            @endif
            <div>
                {{ Form::submit('コメント投稿') }}
            </div>
            {{ Form::token() }}
        {{ Form::close() }}
    @endif
@endsection

これで、動作確認はできます。実際に動きを確認してください。

解説はNo.3で行います。

次記事:速習Larave3(その13)、コメントNo.3