I am learning Laravel and I want to include in my project a 5 star Rating System with AJAX but the POST request is showing a 500 Internal Server Error. This is my controller:
public function favorite()
{
var_dump('tesst');
$this->validate($request, [
'photo_id' => 'required|exists:photos,id',
'rating' => 'required',
]);
$favorite= Favorites::create([
'user_id'=>auth()->id(),
'photo_id'=>request('photo_id'),
'rating'=>request('rating')
]);
return back();
}
and it doesnt enter the var_dump this is the AJAX request:
$('.star').click(function(){
//get the stars index from it id
console.log('test');
var rating = $(this).attr("id").split("-")[1],
photo_id = $("#photo_id").val(), //store the photo id in variable
star_container = $(this).parent(), //get the parent container of the stars
result_div = $("#result"); //result div
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
} ,
url: '/gallery/{id}/detail/'+rating,
type: 'post',
data: {rating:rating,photo_id:photo_id},
beforeSend: function(){
star_container.hide(); //hide the star container
result_div.show().html("Loading..."); //show the result div and display a loadin message
},
success: function(data){
result_div.html(data);
}
});
});
The html
<input type="hidden" value="" id="photo_id">
<div id="star-container">
<i class="fa fa-star fa-3x star" id="star-1"></i>
<i class="fa fa-star fa-3x star" id="star-2"></i>
<i class="fa fa-star fa-3x star" id="star-3"></i>
<i class="fa fa-star fa-3x star" id="star-4"></i>
<i class="fa fa-star fa-3x star" id="star-5"></i>
</div>
<div id="result"> </div>
And the route:
//commenting on picture
Route::get('/gallery/{id}/detail', 'DetailController@index')->name('detail');
Route::post('gallery/{id}/detail','DetailController@store');
//favorites rating section
Route::post('/gallery/{id}/detail/{rating}', 'DetailController@favorite');
Route::get('/gallery/{id}/detail/{rating}', 'DetailController@show');
Can you please tell me what I'm doing wrong? I think it is a route problem.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire