【发布时间】:2019-07-22 19:51:47
【问题描述】:
你好朋友我有这个问题...我有表奖,users_profile..我在哪里保留图像,描述和分数 所以在我的控制器中,我想要做的是以下......如果用户有必要的积分来领取这个奖品,那么他就会认领它,如果他没有足够的积分,那么他会显示一条错误消息
public function reclamarpremios(Request $request)
{
$vago_puntos = request('vago_puntos');
$premioID = request('id');
$puntaje = request('puntaje');
$perfil_users_app_id = request('perfil_users_app_id');
$us = UserAppPerfil::where('id',$perfil_users_app_id);
$reclamo = Premios::where('id',$premioID)->where('puntos','=',$vago_puntos);
if(!$reclamo->exists())
{
$us->decrement('vago_puntos',$puntaje);
$reclamo->increment('veces_reclamado');
return response()->json([$us,$reclamo,'message' => 'Felicidades has reclamado esta promocion, el equipo de vagos estara en contacto con tigo para obtorgarte tu premio'],200);
}else{
return response()->json(['message'=>'No tienes los suficientes vagos puntos'],422);
}
}
这段代码只工作了一半,因为例如,如果用户有 1000 积分,而奖品价值 500 积分,那么他不会交换它(他将我发送到 else)......如果我想要的是在里面某些字段添加和减去某些值,除了显示消息 这是我在 MySQL 数据库中的表
【问题讨论】:
-
所以,我不明白:'如果用户有 1000 积分,而奖品价值 500 积分,那么他不会交换它'。如果用户积分足够,应该可以领取奖品吧?是这个问题吗?
-
是的,这就是问题
-
你为什么要否认这一点: if( ! $reclamo->exists() ) {...} ?因为这个,你要去 else 子句
-
另外:$reclamo 变量的返回值是多少?我认为你应该检查一下。做一个 dd($reclamo);看看返回了什么
-
我这样说(我正在测试),因为如果我让它正常 if ($ reclama-> count ()> 0) ... 代码反过来工作(如果一份礼物价值 500 分,而用户有 200 分,则可以兑换)所以用 if (! $ claim-> count ()> 0) 解决...(我知道这样做是错误的)
标签: php mysql laravel orm eloquent