...
$('#check_ticket').click(function() {
$.ajax({
type: "POST",
url: "{{ Extras::getEUserURLPath(Auth::user()->user_id, '/ajax/setticket') }}",
dataType: 'json',
data: {
ticket: $('#ticket_field').val(),
reservation_id: $('#reservation_id').val(),
user_id: {{ Auth::user()->user_id }},
_token: $('input[name="_token"]').val(),
},
success: function(data) {
if (data.response == true && data.disable == true) {
$('#ticket_field').next().html('Successfully applied ticket!');
$('#ticket_field').prop('disabled', true);
location.reload(true);
...
}
Good day. I wanted to increment the data on table "ticket", column "times_used" whenever I click the "check_ticket" button. Can anyone help me? I think this is a stupid question but Im noob so please go easy on me.
I think perhaps your understanding of the way Laravel works in terms of the MVC framework is missing a little bit -- at least from the question :). From the AJAX method, you want to pass your data to a controller. This controller path would be setup in your routes.php file (< L5.3) or web.php file (L5.3).
That route would look something like:
Route::post('updateCoupon', 'CouponController@increment');
The above shows a route for 'updateCoupon' to be passed to the CouponController controller's increment method. Or, if this is the only thing you want to do with coupons, you could just make a simple closure in the route/web file:
Route::post('updateCoupon', function(){
$couponId = \Input::get('couponId');
$coupon = App\Coupon::find($couponId);
$coupon->times_used ++;
$coupon->save();
});
This is a rough example - the Coupon class would be a 'model' class that you could call and the times_used would be a field within that class. The method in the closure above could be similar to what you'd put in the controller class if you wanted to expand beyond a single method / closure. If you have multiple coupons, pass the id from ajax.
Calling the URL from ajax is easier than what you have above, you can just:
url: "{{url('updateCoupon')}}"
once you have the route set in routes.php/web.php. Laravel's 'url' method magically gets the routing correct.
Bottom line: if all you want to do is increment the times_used in the DB, you don't need to pass anything from ajax other than the couponId, and you don't even need that if you only have one coupon. Then it would just be:
$coupon = App\Coupon::find(1);
And all the rest the same.