Hal*_*nex 8 php nested laravel laravel-5
我正在尝试使用Laravel Commentable来实现多线程注释,它使用嵌套套件和Baum
我已成功地使根评论工作,但是当我回复评论,在数据库中的一条记录被插入不commentable_id和commentable_type所以有在明知该评论的回复是否是没办法App\Post还是App\Product因为这两个领域是空的,我似乎无法理解为什么.
表
users: id, name, email...
posts: id, user_id, subreddit_id...
comments: id, user_id, parent_id, lft, rgt, depth, commentable_id, commentable_type
Run Code Online (Sandbox Code Playgroud)
路线
Route::post('comments/{post}', ['as' => 'comment', 'uses' => 'PostsController@createComment']);
Route::post('comments/{comment}/child', ['as' => 'child-comment', 'uses' => 'PostsController@createChildComment']);
Run Code Online (Sandbox Code Playgroud)
方法 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);
}
Run Code Online (Sandbox Code Playgroud)
查看根评论和子评论
<-- 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() !!}
Run Code Online (Sandbox Code Playgroud)
在我的脑海中,你不会在发表$comment->save()评论之前先创建子项,这样它在使用 . 访问数据库之前就处于正确的状态save。
编辑:试试这个:
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();
}
Run Code Online (Sandbox Code Playgroud)
目前我相信所做的改变$comment->makeChildOf($parent)将会被抛弃。
| 归档时间: |
|
| 查看次数: |
696 次 |
| 最近记录: |