Laravel function controller validation error












2















I have validation issue when I tried to apply the coupon code.
I have to apply on 4 condition




  1. If I applied the correct coupon from the database then it will be fine.

  2. If I applied the correct coupon again from the database then it will be
    show the message 'This Coupon Have Already Used'.

  3. If I just directly key in button (blank value) it will show the message 'Please Insert
    The Coupon Code
    '

  4. But the problem is what I key in the wrong coupon code, I need to
    have 'Wrong Coupon Code Entered' message displayed


Below is the controller part for the function:-



public function checkCoupon(Request $res)
{
$code = $res->code;
$check = DB::table('coupons')
->where('coupon_code',$code)
->get();
if(count($check)=="1") {
$user_id = Auth::user()->id;
$check_used = DB::table('used_coupons')
->where('user_id', $user_id)
->where('coupon_id', $check[0]->id)
->count();

if($check_used=="0"){
$used_add = DB::table('used_coupons')
->insert([
'coupon_id' => $check[0]->id,
'user_id' => $user_id
]);
$insert_cart_total = DB::table('cart_total')
->insert([
'cart_total' => Cart::total(),
'discount' => $check[0]->discount,
'user_id' => $user_id,
'gtotal' => Cart::total() - (Cart::total() * $check[0]->discount)/100,
]);
}
else{
?>
<div class="alert alert-warning">This Coupon Have Already Used</div>
<?php
}
}
else if($code==$check){
?>
<div class="alert alert-danger">Wrong Coupon Code Entered</div>
<?php }
else{
?>
<div class="alert alert-danger">Please Insert The Coupon Code </div>
<?php }
}


JS file for apply coupon function button:-




$(document).ready(function(){           
$('#coupon_btn').click(function(){
var coupon_id = $('#coupon_id').val();
$.ajax({
url:'{{url('/checkCoupon')}}',
data: 'code=' + coupon_id,
success:function(res){
$('#cartTotal').html(res);
}
})
});
});





View File



         <div class="cart-total" >
<h4>Total Amount</h4>
<table>
<tbody>
<tr>
<td>Sub Total</td>
<td>$ <?php echo Cart::subtotal(); ?></td>
</tr>
<tr>
<td>Tax (%)</td>
<td>$ <?php echo Cart::tax(); ?></td>
</tr>


<tr>
<td>Grand Total new</td>
<td>$ <?php echo Cart::total(); ?></td>
</tr>

<tr>
<td>Discount(%) </td>
<td> <?php echo $disnew; ?></td>
</tr>

<tr>
<td>Grand Total (After discount) </td>
<td>$ <?php echo $gtnew; ?></td>
</tr>
</tbody>
</table>
<input type="submit" class="btn update btn-block" style="color: white;font-weight: bold;" value="Continue Shopping">
<a href="<?php echo url('checkout') ?>" class="btn check_out btn-block" style="color: white;font-weight: bold;">Checkout</a>
</div>









share|improve this question




















  • 4





    HTML code printed directly to the screen in controller??? Seriously???

    – jakub wrona
    Nov 16 '18 at 7:19











  • actually the view blade file is fine. just the validation I may make some mistake, so need someone expert who will try to solve this issue. thanks.

    – lun7code
    Nov 16 '18 at 7:34
















2















I have validation issue when I tried to apply the coupon code.
I have to apply on 4 condition




  1. If I applied the correct coupon from the database then it will be fine.

  2. If I applied the correct coupon again from the database then it will be
    show the message 'This Coupon Have Already Used'.

  3. If I just directly key in button (blank value) it will show the message 'Please Insert
    The Coupon Code
    '

  4. But the problem is what I key in the wrong coupon code, I need to
    have 'Wrong Coupon Code Entered' message displayed


Below is the controller part for the function:-



public function checkCoupon(Request $res)
{
$code = $res->code;
$check = DB::table('coupons')
->where('coupon_code',$code)
->get();
if(count($check)=="1") {
$user_id = Auth::user()->id;
$check_used = DB::table('used_coupons')
->where('user_id', $user_id)
->where('coupon_id', $check[0]->id)
->count();

if($check_used=="0"){
$used_add = DB::table('used_coupons')
->insert([
'coupon_id' => $check[0]->id,
'user_id' => $user_id
]);
$insert_cart_total = DB::table('cart_total')
->insert([
'cart_total' => Cart::total(),
'discount' => $check[0]->discount,
'user_id' => $user_id,
'gtotal' => Cart::total() - (Cart::total() * $check[0]->discount)/100,
]);
}
else{
?>
<div class="alert alert-warning">This Coupon Have Already Used</div>
<?php
}
}
else if($code==$check){
?>
<div class="alert alert-danger">Wrong Coupon Code Entered</div>
<?php }
else{
?>
<div class="alert alert-danger">Please Insert The Coupon Code </div>
<?php }
}


JS file for apply coupon function button:-




$(document).ready(function(){           
$('#coupon_btn').click(function(){
var coupon_id = $('#coupon_id').val();
$.ajax({
url:'{{url('/checkCoupon')}}',
data: 'code=' + coupon_id,
success:function(res){
$('#cartTotal').html(res);
}
})
});
});





View File



         <div class="cart-total" >
<h4>Total Amount</h4>
<table>
<tbody>
<tr>
<td>Sub Total</td>
<td>$ <?php echo Cart::subtotal(); ?></td>
</tr>
<tr>
<td>Tax (%)</td>
<td>$ <?php echo Cart::tax(); ?></td>
</tr>


<tr>
<td>Grand Total new</td>
<td>$ <?php echo Cart::total(); ?></td>
</tr>

<tr>
<td>Discount(%) </td>
<td> <?php echo $disnew; ?></td>
</tr>

<tr>
<td>Grand Total (After discount) </td>
<td>$ <?php echo $gtnew; ?></td>
</tr>
</tbody>
</table>
<input type="submit" class="btn update btn-block" style="color: white;font-weight: bold;" value="Continue Shopping">
<a href="<?php echo url('checkout') ?>" class="btn check_out btn-block" style="color: white;font-weight: bold;">Checkout</a>
</div>









share|improve this question




















  • 4





    HTML code printed directly to the screen in controller??? Seriously???

    – jakub wrona
    Nov 16 '18 at 7:19











  • actually the view blade file is fine. just the validation I may make some mistake, so need someone expert who will try to solve this issue. thanks.

    – lun7code
    Nov 16 '18 at 7:34














2












2








2


1






I have validation issue when I tried to apply the coupon code.
I have to apply on 4 condition




  1. If I applied the correct coupon from the database then it will be fine.

  2. If I applied the correct coupon again from the database then it will be
    show the message 'This Coupon Have Already Used'.

  3. If I just directly key in button (blank value) it will show the message 'Please Insert
    The Coupon Code
    '

  4. But the problem is what I key in the wrong coupon code, I need to
    have 'Wrong Coupon Code Entered' message displayed


Below is the controller part for the function:-



public function checkCoupon(Request $res)
{
$code = $res->code;
$check = DB::table('coupons')
->where('coupon_code',$code)
->get();
if(count($check)=="1") {
$user_id = Auth::user()->id;
$check_used = DB::table('used_coupons')
->where('user_id', $user_id)
->where('coupon_id', $check[0]->id)
->count();

if($check_used=="0"){
$used_add = DB::table('used_coupons')
->insert([
'coupon_id' => $check[0]->id,
'user_id' => $user_id
]);
$insert_cart_total = DB::table('cart_total')
->insert([
'cart_total' => Cart::total(),
'discount' => $check[0]->discount,
'user_id' => $user_id,
'gtotal' => Cart::total() - (Cart::total() * $check[0]->discount)/100,
]);
}
else{
?>
<div class="alert alert-warning">This Coupon Have Already Used</div>
<?php
}
}
else if($code==$check){
?>
<div class="alert alert-danger">Wrong Coupon Code Entered</div>
<?php }
else{
?>
<div class="alert alert-danger">Please Insert The Coupon Code </div>
<?php }
}


JS file for apply coupon function button:-




$(document).ready(function(){           
$('#coupon_btn').click(function(){
var coupon_id = $('#coupon_id').val();
$.ajax({
url:'{{url('/checkCoupon')}}',
data: 'code=' + coupon_id,
success:function(res){
$('#cartTotal').html(res);
}
})
});
});





View File



         <div class="cart-total" >
<h4>Total Amount</h4>
<table>
<tbody>
<tr>
<td>Sub Total</td>
<td>$ <?php echo Cart::subtotal(); ?></td>
</tr>
<tr>
<td>Tax (%)</td>
<td>$ <?php echo Cart::tax(); ?></td>
</tr>


<tr>
<td>Grand Total new</td>
<td>$ <?php echo Cart::total(); ?></td>
</tr>

<tr>
<td>Discount(%) </td>
<td> <?php echo $disnew; ?></td>
</tr>

<tr>
<td>Grand Total (After discount) </td>
<td>$ <?php echo $gtnew; ?></td>
</tr>
</tbody>
</table>
<input type="submit" class="btn update btn-block" style="color: white;font-weight: bold;" value="Continue Shopping">
<a href="<?php echo url('checkout') ?>" class="btn check_out btn-block" style="color: white;font-weight: bold;">Checkout</a>
</div>









share|improve this question
















I have validation issue when I tried to apply the coupon code.
I have to apply on 4 condition




  1. If I applied the correct coupon from the database then it will be fine.

  2. If I applied the correct coupon again from the database then it will be
    show the message 'This Coupon Have Already Used'.

  3. If I just directly key in button (blank value) it will show the message 'Please Insert
    The Coupon Code
    '

  4. But the problem is what I key in the wrong coupon code, I need to
    have 'Wrong Coupon Code Entered' message displayed


Below is the controller part for the function:-



public function checkCoupon(Request $res)
{
$code = $res->code;
$check = DB::table('coupons')
->where('coupon_code',$code)
->get();
if(count($check)=="1") {
$user_id = Auth::user()->id;
$check_used = DB::table('used_coupons')
->where('user_id', $user_id)
->where('coupon_id', $check[0]->id)
->count();

if($check_used=="0"){
$used_add = DB::table('used_coupons')
->insert([
'coupon_id' => $check[0]->id,
'user_id' => $user_id
]);
$insert_cart_total = DB::table('cart_total')
->insert([
'cart_total' => Cart::total(),
'discount' => $check[0]->discount,
'user_id' => $user_id,
'gtotal' => Cart::total() - (Cart::total() * $check[0]->discount)/100,
]);
}
else{
?>
<div class="alert alert-warning">This Coupon Have Already Used</div>
<?php
}
}
else if($code==$check){
?>
<div class="alert alert-danger">Wrong Coupon Code Entered</div>
<?php }
else{
?>
<div class="alert alert-danger">Please Insert The Coupon Code </div>
<?php }
}


JS file for apply coupon function button:-




$(document).ready(function(){           
$('#coupon_btn').click(function(){
var coupon_id = $('#coupon_id').val();
$.ajax({
url:'{{url('/checkCoupon')}}',
data: 'code=' + coupon_id,
success:function(res){
$('#cartTotal').html(res);
}
})
});
});





View File



         <div class="cart-total" >
<h4>Total Amount</h4>
<table>
<tbody>
<tr>
<td>Sub Total</td>
<td>$ <?php echo Cart::subtotal(); ?></td>
</tr>
<tr>
<td>Tax (%)</td>
<td>$ <?php echo Cart::tax(); ?></td>
</tr>


<tr>
<td>Grand Total new</td>
<td>$ <?php echo Cart::total(); ?></td>
</tr>

<tr>
<td>Discount(%) </td>
<td> <?php echo $disnew; ?></td>
</tr>

<tr>
<td>Grand Total (After discount) </td>
<td>$ <?php echo $gtnew; ?></td>
</tr>
</tbody>
</table>
<input type="submit" class="btn update btn-block" style="color: white;font-weight: bold;" value="Continue Shopping">
<a href="<?php echo url('checkout') ?>" class="btn check_out btn-block" style="color: white;font-weight: bold;">Checkout</a>
</div>





$(document).ready(function(){           
$('#coupon_btn').click(function(){
var coupon_id = $('#coupon_id').val();
$.ajax({
url:'{{url('/checkCoupon')}}',
data: 'code=' + coupon_id,
success:function(res){
$('#cartTotal').html(res);
}
})
});
});





$(document).ready(function(){           
$('#coupon_btn').click(function(){
var coupon_id = $('#coupon_id').val();
$.ajax({
url:'{{url('/checkCoupon')}}',
data: 'code=' + coupon_id,
success:function(res){
$('#cartTotal').html(res);
}
})
});
});






php laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 12:44







lun7code

















asked Nov 16 '18 at 7:08









lun7codelun7code

548




548








  • 4





    HTML code printed directly to the screen in controller??? Seriously???

    – jakub wrona
    Nov 16 '18 at 7:19











  • actually the view blade file is fine. just the validation I may make some mistake, so need someone expert who will try to solve this issue. thanks.

    – lun7code
    Nov 16 '18 at 7:34














  • 4





    HTML code printed directly to the screen in controller??? Seriously???

    – jakub wrona
    Nov 16 '18 at 7:19











  • actually the view blade file is fine. just the validation I may make some mistake, so need someone expert who will try to solve this issue. thanks.

    – lun7code
    Nov 16 '18 at 7:34








4




4





HTML code printed directly to the screen in controller??? Seriously???

– jakub wrona
Nov 16 '18 at 7:19





HTML code printed directly to the screen in controller??? Seriously???

– jakub wrona
Nov 16 '18 at 7:19













actually the view blade file is fine. just the validation I may make some mistake, so need someone expert who will try to solve this issue. thanks.

– lun7code
Nov 16 '18 at 7:34





actually the view blade file is fine. just the validation I may make some mistake, so need someone expert who will try to solve this issue. thanks.

– lun7code
Nov 16 '18 at 7:34












1 Answer
1






active

oldest

votes


















5














To check used coupons better to use exists instead of count.



To get coupon better to use first instead of get (first - return first element, you dont need to use index on collection)



I think, in this situation better to use returns. Code and logic looks clearly.



public function checkCoupon(Request $res)
{
$code = $res->input('code');
$userId = Auth::user()->id;

if(!$code){
return ['error' => '<div class="alert alert-danger">Please Insert The Coupon Code </div>'];
}

$coupon = DB::table('coupons')
->where('coupon_code', $code)
->first();

if(!$coupon) {
return ['error' => '<div class="alert alert-danger">Wrong Coupon Code Entered</div>'];
}

$isUsed = DB::table('used_coupons')
->where('user_id', $userId)
->where('coupon_id', $coupon->id)
->exists();

if($isUsed){
return ['error' => '<div class="alert alert-warning">This Coupon Have Already Used</div>'];
}

DB::table('used_coupons')
->insert([
'coupon_id' => $coupon->id,
'user_id' => $userId
]);

DB::table('cart_total')
->insert([
'cart_total' => Cart::total(),
'discount' => $coupon->discount,
'user_id' => $userId,
'gtotal' => Cart::total() - (Cart::total() * $coupon->discount)/100,
]);

return [
'subtotal' => Cart::subtotal(),
'total' => Cart::total()
];
}


Jquery:



$(document).ready(function(){           
$('#coupon_btn').click(function(){
var coupon_id = $('#coupon_id').val();
$.ajax({
url:'{{url('/checkCoupon')}}',
dataType: "json",
data: {code: coupon_id},
success:function(res){
if(res.error){
$('...error_selector...').html(res.error);
} else {
$('...total_selector...').html(res.total);
$('...subtotal_selector...').html(res.subtotal);
}

}
})
});
});





share|improve this answer


























  • may i know is that possible to insert <?php echo ?> inside the return ?

    – lun7code
    Nov 16 '18 at 7:55











  • For what? It will only print <?php echo ?> as text without execution.

    – IndianCoding
    Nov 16 '18 at 8:03













  • let say I want to <?php echo Cart::subtotal(); ?> the Cart is the model. I wish to print out the subtotal of cart model.

    – lun7code
    Nov 16 '18 at 8:09











  • I think you can add "return Cart::subtotal();" in the end of method.

    – IndianCoding
    Nov 16 '18 at 8:22











  • can you briefly describe how to make it?

    – lun7code
    Nov 16 '18 at 8:37











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333029%2flaravel-function-controller-validation-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














To check used coupons better to use exists instead of count.



To get coupon better to use first instead of get (first - return first element, you dont need to use index on collection)



I think, in this situation better to use returns. Code and logic looks clearly.



public function checkCoupon(Request $res)
{
$code = $res->input('code');
$userId = Auth::user()->id;

if(!$code){
return ['error' => '<div class="alert alert-danger">Please Insert The Coupon Code </div>'];
}

$coupon = DB::table('coupons')
->where('coupon_code', $code)
->first();

if(!$coupon) {
return ['error' => '<div class="alert alert-danger">Wrong Coupon Code Entered</div>'];
}

$isUsed = DB::table('used_coupons')
->where('user_id', $userId)
->where('coupon_id', $coupon->id)
->exists();

if($isUsed){
return ['error' => '<div class="alert alert-warning">This Coupon Have Already Used</div>'];
}

DB::table('used_coupons')
->insert([
'coupon_id' => $coupon->id,
'user_id' => $userId
]);

DB::table('cart_total')
->insert([
'cart_total' => Cart::total(),
'discount' => $coupon->discount,
'user_id' => $userId,
'gtotal' => Cart::total() - (Cart::total() * $coupon->discount)/100,
]);

return [
'subtotal' => Cart::subtotal(),
'total' => Cart::total()
];
}


Jquery:



$(document).ready(function(){           
$('#coupon_btn').click(function(){
var coupon_id = $('#coupon_id').val();
$.ajax({
url:'{{url('/checkCoupon')}}',
dataType: "json",
data: {code: coupon_id},
success:function(res){
if(res.error){
$('...error_selector...').html(res.error);
} else {
$('...total_selector...').html(res.total);
$('...subtotal_selector...').html(res.subtotal);
}

}
})
});
});





share|improve this answer


























  • may i know is that possible to insert <?php echo ?> inside the return ?

    – lun7code
    Nov 16 '18 at 7:55











  • For what? It will only print <?php echo ?> as text without execution.

    – IndianCoding
    Nov 16 '18 at 8:03













  • let say I want to <?php echo Cart::subtotal(); ?> the Cart is the model. I wish to print out the subtotal of cart model.

    – lun7code
    Nov 16 '18 at 8:09











  • I think you can add "return Cart::subtotal();" in the end of method.

    – IndianCoding
    Nov 16 '18 at 8:22











  • can you briefly describe how to make it?

    – lun7code
    Nov 16 '18 at 8:37
















5














To check used coupons better to use exists instead of count.



To get coupon better to use first instead of get (first - return first element, you dont need to use index on collection)



I think, in this situation better to use returns. Code and logic looks clearly.



public function checkCoupon(Request $res)
{
$code = $res->input('code');
$userId = Auth::user()->id;

if(!$code){
return ['error' => '<div class="alert alert-danger">Please Insert The Coupon Code </div>'];
}

$coupon = DB::table('coupons')
->where('coupon_code', $code)
->first();

if(!$coupon) {
return ['error' => '<div class="alert alert-danger">Wrong Coupon Code Entered</div>'];
}

$isUsed = DB::table('used_coupons')
->where('user_id', $userId)
->where('coupon_id', $coupon->id)
->exists();

if($isUsed){
return ['error' => '<div class="alert alert-warning">This Coupon Have Already Used</div>'];
}

DB::table('used_coupons')
->insert([
'coupon_id' => $coupon->id,
'user_id' => $userId
]);

DB::table('cart_total')
->insert([
'cart_total' => Cart::total(),
'discount' => $coupon->discount,
'user_id' => $userId,
'gtotal' => Cart::total() - (Cart::total() * $coupon->discount)/100,
]);

return [
'subtotal' => Cart::subtotal(),
'total' => Cart::total()
];
}


Jquery:



$(document).ready(function(){           
$('#coupon_btn').click(function(){
var coupon_id = $('#coupon_id').val();
$.ajax({
url:'{{url('/checkCoupon')}}',
dataType: "json",
data: {code: coupon_id},
success:function(res){
if(res.error){
$('...error_selector...').html(res.error);
} else {
$('...total_selector...').html(res.total);
$('...subtotal_selector...').html(res.subtotal);
}

}
})
});
});





share|improve this answer


























  • may i know is that possible to insert <?php echo ?> inside the return ?

    – lun7code
    Nov 16 '18 at 7:55











  • For what? It will only print <?php echo ?> as text without execution.

    – IndianCoding
    Nov 16 '18 at 8:03













  • let say I want to <?php echo Cart::subtotal(); ?> the Cart is the model. I wish to print out the subtotal of cart model.

    – lun7code
    Nov 16 '18 at 8:09











  • I think you can add "return Cart::subtotal();" in the end of method.

    – IndianCoding
    Nov 16 '18 at 8:22











  • can you briefly describe how to make it?

    – lun7code
    Nov 16 '18 at 8:37














5












5








5







To check used coupons better to use exists instead of count.



To get coupon better to use first instead of get (first - return first element, you dont need to use index on collection)



I think, in this situation better to use returns. Code and logic looks clearly.



public function checkCoupon(Request $res)
{
$code = $res->input('code');
$userId = Auth::user()->id;

if(!$code){
return ['error' => '<div class="alert alert-danger">Please Insert The Coupon Code </div>'];
}

$coupon = DB::table('coupons')
->where('coupon_code', $code)
->first();

if(!$coupon) {
return ['error' => '<div class="alert alert-danger">Wrong Coupon Code Entered</div>'];
}

$isUsed = DB::table('used_coupons')
->where('user_id', $userId)
->where('coupon_id', $coupon->id)
->exists();

if($isUsed){
return ['error' => '<div class="alert alert-warning">This Coupon Have Already Used</div>'];
}

DB::table('used_coupons')
->insert([
'coupon_id' => $coupon->id,
'user_id' => $userId
]);

DB::table('cart_total')
->insert([
'cart_total' => Cart::total(),
'discount' => $coupon->discount,
'user_id' => $userId,
'gtotal' => Cart::total() - (Cart::total() * $coupon->discount)/100,
]);

return [
'subtotal' => Cart::subtotal(),
'total' => Cart::total()
];
}


Jquery:



$(document).ready(function(){           
$('#coupon_btn').click(function(){
var coupon_id = $('#coupon_id').val();
$.ajax({
url:'{{url('/checkCoupon')}}',
dataType: "json",
data: {code: coupon_id},
success:function(res){
if(res.error){
$('...error_selector...').html(res.error);
} else {
$('...total_selector...').html(res.total);
$('...subtotal_selector...').html(res.subtotal);
}

}
})
});
});





share|improve this answer















To check used coupons better to use exists instead of count.



To get coupon better to use first instead of get (first - return first element, you dont need to use index on collection)



I think, in this situation better to use returns. Code and logic looks clearly.



public function checkCoupon(Request $res)
{
$code = $res->input('code');
$userId = Auth::user()->id;

if(!$code){
return ['error' => '<div class="alert alert-danger">Please Insert The Coupon Code </div>'];
}

$coupon = DB::table('coupons')
->where('coupon_code', $code)
->first();

if(!$coupon) {
return ['error' => '<div class="alert alert-danger">Wrong Coupon Code Entered</div>'];
}

$isUsed = DB::table('used_coupons')
->where('user_id', $userId)
->where('coupon_id', $coupon->id)
->exists();

if($isUsed){
return ['error' => '<div class="alert alert-warning">This Coupon Have Already Used</div>'];
}

DB::table('used_coupons')
->insert([
'coupon_id' => $coupon->id,
'user_id' => $userId
]);

DB::table('cart_total')
->insert([
'cart_total' => Cart::total(),
'discount' => $coupon->discount,
'user_id' => $userId,
'gtotal' => Cart::total() - (Cart::total() * $coupon->discount)/100,
]);

return [
'subtotal' => Cart::subtotal(),
'total' => Cart::total()
];
}


Jquery:



$(document).ready(function(){           
$('#coupon_btn').click(function(){
var coupon_id = $('#coupon_id').val();
$.ajax({
url:'{{url('/checkCoupon')}}',
dataType: "json",
data: {code: coupon_id},
success:function(res){
if(res.error){
$('...error_selector...').html(res.error);
} else {
$('...total_selector...').html(res.total);
$('...subtotal_selector...').html(res.subtotal);
}

}
})
});
});






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 10:03

























answered Nov 16 '18 at 7:32









IndianCodingIndianCoding

1,0441110




1,0441110













  • may i know is that possible to insert <?php echo ?> inside the return ?

    – lun7code
    Nov 16 '18 at 7:55











  • For what? It will only print <?php echo ?> as text without execution.

    – IndianCoding
    Nov 16 '18 at 8:03













  • let say I want to <?php echo Cart::subtotal(); ?> the Cart is the model. I wish to print out the subtotal of cart model.

    – lun7code
    Nov 16 '18 at 8:09











  • I think you can add "return Cart::subtotal();" in the end of method.

    – IndianCoding
    Nov 16 '18 at 8:22











  • can you briefly describe how to make it?

    – lun7code
    Nov 16 '18 at 8:37



















  • may i know is that possible to insert <?php echo ?> inside the return ?

    – lun7code
    Nov 16 '18 at 7:55











  • For what? It will only print <?php echo ?> as text without execution.

    – IndianCoding
    Nov 16 '18 at 8:03













  • let say I want to <?php echo Cart::subtotal(); ?> the Cart is the model. I wish to print out the subtotal of cart model.

    – lun7code
    Nov 16 '18 at 8:09











  • I think you can add "return Cart::subtotal();" in the end of method.

    – IndianCoding
    Nov 16 '18 at 8:22











  • can you briefly describe how to make it?

    – lun7code
    Nov 16 '18 at 8:37

















may i know is that possible to insert <?php echo ?> inside the return ?

– lun7code
Nov 16 '18 at 7:55





may i know is that possible to insert <?php echo ?> inside the return ?

– lun7code
Nov 16 '18 at 7:55













For what? It will only print <?php echo ?> as text without execution.

– IndianCoding
Nov 16 '18 at 8:03







For what? It will only print <?php echo ?> as text without execution.

– IndianCoding
Nov 16 '18 at 8:03















let say I want to <?php echo Cart::subtotal(); ?> the Cart is the model. I wish to print out the subtotal of cart model.

– lun7code
Nov 16 '18 at 8:09





let say I want to <?php echo Cart::subtotal(); ?> the Cart is the model. I wish to print out the subtotal of cart model.

– lun7code
Nov 16 '18 at 8:09













I think you can add "return Cart::subtotal();" in the end of method.

– IndianCoding
Nov 16 '18 at 8:22





I think you can add "return Cart::subtotal();" in the end of method.

– IndianCoding
Nov 16 '18 at 8:22













can you briefly describe how to make it?

– lun7code
Nov 16 '18 at 8:37





can you briefly describe how to make it?

– lun7code
Nov 16 '18 at 8:37




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333029%2flaravel-function-controller-validation-error%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python