When I'm doing a request in Ajax, it's throwing me this error.
This is the new error that it's throwing to me:
"message": "Undefined variable: user_id",
"exception": "ErrorException",
"file": "/Users/danhergir/Desktop/startup/app/Http/Controllers/ProductReviewController.php",
"line": 113,
"trace":
The error is in the line:
$like->user_id = $user_id;
What can I do for solving it?
This is my route.
Web.php
Route::post('/like', [
'uses' => 'ProductReviewController@postLike',
'as' => 'like'
]);
This is my view file. (This is the part where I have the like/dislike buttons)
Show.blade.php
<div class="container">
<div class="row">
<div class="col-md-6" id="user-reviews">
<h3>Recent comments</h3>
@forelse($product->reviews as $review)
<div class="mt-5 border border-dark pl-3 pt-3 pb-3 mb-3 rounded reviewid" data-reviewid="{{ $review->id }}">
<div class="title">
<h4>{{ $review->headline }}</h4>
</div>
<div class="user-rating">
<star-rating class="pr-3" :star-size="20" :read-only="true" :show-rating="false" :rating="{{ $review->rating }}"></star-rating>
</div>
<div class="body-text pt-3 pr-5">
<p style="text-align:justify"><strong>{{ $review->description }}</strong></p>
</div>
<div class="body-text pt-3">
<h6>
<a href="#" class="btn btn-xs btn-warning like">Like</a>
<a href="#" class="btn btn-xs btn-danger like">Dislike</a>
</h6>
</div>
<div class="author pt-2">
<h6 class="text-muted">{{ $review->user_name }}, {{ date('d-m-Y', strtotime( $review->created_at )) }}</h6>
</div>
</div>
@empty
<h6>There are not reviews for this product</h6>
@endforelse
</div>
</div>
</div>
</div>
@endsection
@section('scripts')
<script src="{{ asset('js/like.js') }}"></script>
<script type="text/javascript">
var token = '{{ Session::token() }}';
var urlLike = '{{ route('like') }}';
</script>
@endsection
And finally, this is my JS file.
Like.js
$('.like').on('click', function(event) {
event.preventDefault();
reviewId = $(this).closest(".reviewid").attr("data-reviewid");;
var isLike = event.target.previousElementSibling == null;
$.ajax({
method: 'POST',
url: urlLike,
data: {isLike: isLike, reviewId: reviewId, _token:token},
})
});