So, I created a report system, but in the last step, I have a little problem...In JavaScript I'm using a variable from database for Ajax, but when a post has no comments, I'm getting Undefined variable: key.
Here is my script
if (isset($key)){
$('#yourFormIdComment{{$key+1}}').submit(function(e){
e.preventDefault();
var data = $(this).serialize();
$.ajax({
method:'POST',
url:'/career_report_comment',
data:data,
success: function (result) {
// do something with result
}
});
});
}
Here is my view with variable
@foreach($opinionComment as $key => $comments)
<div class="news-v3 bg-color-white">
<h4 style="font-size: 13px">
{{ $comments->user->username }}</h4>
<p>{{ $comments->comments }}</p>
<ul class="post-shares post-shares-lg">
<li @if ($key === 0) class="active" @endif style="float: right;left: 20px;bottom: 30px"><a data-toggle="modal" data-target="#exampleModalComment{{$key+1}}" href="javascript:void(0)" ><i class="rounded-x icon-flag"></i></a></li>
</ul>
</div>
@endforeach
My question is...how can I pass the variable {{$key+1}}from script even if a post has no reviews/comments?Because this is happening only when I have comments on a post.
if ( $key == null( { code }, but this isn't working.