我在这里尝试做的是在插入后我想在同一页面中显示数据而不刷新页面
路线
Route :: post('viewBook / {bookId}','CommentsController @ insertcomment');
//这个http://localhost:8000/viewBook/1
的{bookId}原因CONTROLLER
public function insertcomment(Request $request)
{
$commenter = Auth::user()->id;
$comments = new Comments;
$comments->comment = $request->input('mycomment');
$comments->commenter = $commenter;
$comments->save(); //insert success
//getting the data
$data=DB::table('comments')
->select('comment')
->get();
return response()->json($data,true);
}
答案 0 :(得分:0)
您不需要再次从DB获取数据,只需返回插入的Comment
对象:
$commenter = Auth::user()->id;
$comment = new Comments;
$comment->comment = $request->input('mycomment');
$comment->commenter = $commenter;
$comment->save(); //insert success
return response()->json($comment, true);
答案 1 :(得分:0)
在你的情况下。