I am trying to get multi-threaded comments using Laravel Commentable which uses nested sets with Baum
I managed to get the root comments to work, however, when I respond to the comment, the database record is inserted without commentable_id and commentable_type , so there is no way to find out if the answer matches this comment App\Post or App\Product , because these 2 fields are empty. and I canβt understand why.
Table
users: id, name, email... posts: id, user_id, subreddit_id... comments: id, user_id, parent_id, lft, rgt, depth, commentable_id, commentable_type
Routes
Route::post('comments/{post}', ['as' => 'comment', 'uses' => 'PostsController@createComment']); Route::post('comments/{comment}/child', ['as' => 'child-comment', 'uses' => 'PostsController@createChildComment']);
Methods in 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); }
View root comments and comments for kids
<-- 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() !!}
php nested laravel laravel-5
Halnex
source share