I am creating a visit system where people can visit many places and i have created the code with ajax and laravel that allows for a button to be clicked to confirm a user has visited a place and they can also undo this by clicking the same button. I want to convert the code to follow so that i also have the following but my AJAX JS skills are limited:
- I want a live count so that if the user has visited a place it will add to the count and be updated to tell how many people have visited the place
- i Want to change the button if the user has visited the place to show that the button has been clicked and they have visited.
Here is what i have so far:
PHP:
$place_id = $request['place_id'];
$place = place::find($place_id);
$visited = Auth::user()->visitors()->where('place_id', $place_id)->first();
if($visited == null) {
$visited = new Visit();
$visited->user_id = Auth::user();
$visited->place_id = $place->id;
$visited->save();
return null;
}
else{
$visited->delete();
return null;
}
HTML:
<a class="btn btn-not-visited visit" data-id="">Visited?</a>
@if($place->visitors->count() == 1)
<h5 class="visitor-count"> Visitor has visited this place</h5>
@else
<h5 class="visitor-count"> visitors have visited this place</h5>
@endif
Ajax JS:
var token = '';
var urlVisit = '';
$('.visit').on('click', function (event) {
event.preventDefault();
$.ajax({
method: 'POST',
url: urlVisit,
data: {
place_id: $(event.target).data("id"),
_token: token
}
}).done(function() {
//
});
});
Here is the button i want to show if the user has visited a place
<a class="btn btn-visited visit" data-id="">I've Visited</a>
how do i add to this code to achieve both bullet points?
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2IcJwPT
via IFTTT
Aucun commentaire:
Enregistrer un commentaire