[英]how to make a condition in the controller with laravel 5.8?
你好朋友,我有这个问题...我有桌子奖,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点积分,那么他就不会交换它(他将我发送给其他人)...如果需要,请在内部添加某些字段并减去某些值,除了显示消息,这是我在MySQL数据库中的表
我无法理解此行中的第二个条件( ->where('puntos','=',$vago_puntos)
):
$reclamo = Premios::where('id',$premioID)->where('puntos','=',$vago_puntos);
用户要兑换的奖品是id
等于$premioID
,不是吗? 如果是这样,则您不需要该条件。
public function reclamarpremios(Request $request)
{
$vago_puntos = request('vago_puntos');
$premioID = request('id'); //The prize id
$puntaje = request('puntaje');
$perfil_users_app_id = request('perfil_users_app_id');
$us = UserAppPerfil::where('id',$perfil_users_app_id)->first();
//Get the prize the user wants to redeem:
$reclamo = Premios::where('id',$premioID)->first();
if ($reclamo != null) { // if the prize exists
$requiredPoints = $reclamo->puntos; //The required amount of points for that prize
$userPoints = $us->puntos; //The user available points
if ($userPoints >= $requiredPoints) {
// if the user has the required amount of points, can redeem the prize:
$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);
}
}else{
return response()->json(['message' => 'This prize is not available']);
}
}
希望能帮助到你。
朋友尝试了很多次后解决
public function reclamarpremios(Request $request)
{
$vago_puntos = request('vago_puntos');
$premioID = request('id');
$perfil_users_app_id = request('perfil_users_app_id');
$us = UserAppPerfil::where('id',$perfil_users_app_id)->first();
if($reclamo = Premios::where('id',$premioID)->where('puntos','<=',$us->vago_puntos)->first())
{
$us->decrement('vago_puntos',$reclamo->puntos);
$us->increment('premios_ganados');
$reclamo->increment('veces_reclamado');
return response()->json([$reclamo,$us,'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);
}
}
我只是有点怀疑...这是否意味着> =更大且相等,是否意味着<=更少且相等?....可笑的是,如果我这样离开它,就等于> = ...我的想法是,尽管我冲了200分,奖品价值500,但我可以索取,如果我的总分超过500,那么我就做不到,如果我留下它<=,那么它是否符合我想要的逻辑。 ...我不知道有人可以向我解释为什么会这样吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.