2015-10-13 7 views
8

commentable IDとタイプすることなく、挿入されていますしかし、私がコメントに返信すると、データベース内のレコードはcommentable_idcommentable_typeなしで挿入されるので、そのコメントへの返信がApp\PostまたはApp\Productであるかどうかを知る方法はありません。理由を理解する。ネストされたセット:子供たちのコメントは、私は、ルートのコメントを作るために、私が管理している<a href="https://github.com/etrepat/baum" rel="noreferrer">Baum</a></p> <p>でネストされたセットを使用<a href="https://github.com/RomainLanz/laravel-commentable" rel="noreferrer">Laravel Commentable</a>を使用してマルチスレッドのコメントを達成しようとしています

テーブル

users: id, name, email... 
posts: id, user_id, subreddit_id... 
comments: id, user_id, parent_id, lft, rgt, depth, commentable_id, commentable_type 

ルート

Route::post('comments/{post}', ['as' => 'comment', 'uses' => '[email protected]']); 
Route::post('comments/{comment}/child', ['as' => 'child-comment', 'uses' => '[email protected]']); 

PostController

public function createComment($id) { 

    $post = Post::with('user.votes')->with('subreddit.moderators')->where('id', $id)->first(); 

    $comment = new Comment; 
    $comment->body = Input::get('comment'); 
    $comment->user_id = Auth::id(); 

    $post->comments()->save($comment); 
} 

public function createChildComment(Post $post){ 
    $parent = Comment::find(Input::get('parent_id')); 

    $comment = new Comment; 
    $comment->body = Input::get('child-comment'); 
    $comment->user_id = Auth::id(); 
    $comment->save(); 
    $comment->makeChildOf($parent); 

} 

のメソッドルートのコメントと子供のためのビューが

<-- This is for root comments --/> 
{!! Form::open(['route' => ['comment', $post]]) !!} 
@foreach($comments as $comment) 

@endforeach 
{!! Form::close() !!} 

<-- This is for children comments --/> 
{!! Form::open(['route' => ['child-comment', $comment]]) !!} 
<input type="hidden" name="parent_id" value="{{ $comment->id }}"/> 
{!! Form::close() !!} 
コメント
+0

投稿とコメントのモデルも表示する必要があります – user2479930

答えて

1

あなたの前に子供を作ってくれないでしょうか?$comment->save()コメントは正しいので、それはsaveでデータベースに当たる前です。

編集:現在、私は$comment->makeChildOf($parent)がないこと変更がスローになるだろうという信念のもとだ

public function createChildComment(Post $post){ 
    $parent = Comment::find(Input::get('parent_id')); 

    $comment = new Comment; 
    $comment->body = Input::get('child-comment'); 
    $comment->user_id = Auth::id(); 
    $comment->save(); 
    $comment->makeChildOf($parent); 
    $comment->save(); 

} 

:これを試してみてください。

+0

「新しいノードを移動できません」というエラーが表示されますので、「makeChildOf」の前に保存しています。 – Halnex

+0

追加の保存を追加しました。私が考えているのは –

+0

なのですか? –

関連する問題

 関連する問題