表单代码更新。
@foreach($posts as $post) <p>$post->description</p> <div ></div> // I found this pre on laracast, at end there is link. @if (count($post->comments)) @foreach($post->commnents as $comment) <small>$comment->body</small> @endforeach @else No comments Found @endif
将数据存储在json响应中。
if($request->ajax()){ $comment = new Comment; $comment->user_id = Auth::user()->id; $comment->post_id = $post_id; $comment->body = Input::get('body'); $comment->save(); //add the comment in response return response()->json(['msg'=>$comment->body]);}在ajax中需要显示消息,我们已经成功添加了数据
$.ajax({ type: "POST", url: '/comment/'+post_id, data: {body:body, post_id:post_id, user_id:user_id, _token: '{{csrf_token()}}'}, success: function(data) { //get the msg from success response. $(".show_comments_"+post_id).append("<div style = 'color:red;'>"+data.msg+"</div>"); } });


