Nested kits with Baum and Laravel Commentable: children comments are inserted without comment identifier and type

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() !!} 
+8
php nested laravel laravel-5
source share
1 answer

On top of my head, do you make a child before you $comment->save() , so that it is in the correct state before it gets to the database using save .

Edit: Try the following:

 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(); } 

I am currently convinced that the change that $comment->makeChildOf($parent) will make will be thrown.

+1
source share

All Articles