Hi i'm currently develop comment function in laravel 5. I want to display instantly the comment the user just post.So how can i achieve that ? I have try to use load method in ajax but still not work
Here is the controller to insert comment
public function newComment(Request $request)
{
try{
$date_format = date('Y-m-d');
$comment=new Comment();
$comment->user_id=Auth::user()->id;
$comment->introduce=$request->introduce;
$comment->completed_day=$request->completed_day;
$comment->allowance=str_replace( ',', '', $request->allowance);
$comment->post_at=$date_format;
$comment->job_id=$request->job_id;
$comment->save();
return response()->json(array('mess'=>'Success'));
}
catch(Exception $ex){
return response()->json(array('err'=>'Error'));
}
}
Here is the view and {{$jobReply -> user -> full_name }} im using ORM to get user name
<div class="panel-body" id="job_comment_post" style="text-align:left">
<table class="table table-hover" >
<thead>
<tr>
<th>Freelancer name</th>
<th>Introduce</th>
<th>Completed day</th>
<th>Allowance</th>
</tr>
</thead>
<tbody>
@foreach($job_comment as $jobReply)
<tr>
<td>{{$jobReply -> user -> full_name }}</td>
<td>{{$jobReply -> introduce}}</td>
<td>{{$jobReply -> completed_day}}</td>
<td>{{number_format($jobReply -> allowance)}}</td>
</tr>
@endforeach()
</tbody>
</table>
<div class="details_pagi">
{!! $job_comment->render() !!}
</div>
</div>
This is ajax to handle insert comment
$(document).ready(function() {
$('#btnInsertComment').click(function(event) {
event.preventDefault();
var data=$("#commentForm").serialize();
$.ajax({
url: '/postComment',
type: 'POST',
data: data,
success:function(data) {
alert(data.mess);
//job_comment_post is the div i want to load new comment
$("#job_comment_post").load();
$("#commentForm")[0].reset();
},
error:function(data) {
alert(data.err);
}
});
});
});
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1RGisue
via IFTTT
You probably figured it out, but in order to use `data` in the `success callback of the AJAX function, your newComment() method must echo the JSON encoded data, not return it (remember to exit() after echoing).
RépondreSupprimerAlso, json() should be json_encode().
So off the top of my head, I would try:
`public function newComment(Request $request)
{
try{
$date_format = date('Y-m-d');
$comment=new Comment();
$comment->user_id=Auth::user()->id;
$comment->introduce=$request->introduce;
$comment->completed_day=$request->completed_day;
$comment->allowance=str_replace( ',', '', $request->allowance);
$comment->post_at=$date_format;
$comment->job_id=$request->job_id;
$comment->save();
echo response()->json_encode(array('mess'=>'Success'));
exit();
}
catch(Exception $ex){
return response()->json(array('err'=>'Error'));
}
return false;
}`