php - 如何使用 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 分,那么他不会交换它(他把我送到其他地方)......如果我想要的是在某些领域添加并减去某些值,除了显示消息 this is my table in 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']);
}
}
希望能帮助到你。
推荐阅读
- rust - 将泛型类型限制为原始数字类型
- google-chrome - 紧循环后如何恢复 Chrome?
- json - SQL Server:如何将所有行作为 Json 对象查询到其他列旁边?
- java - 你如何使生成的构建战争包含基于 Git 提交 ID 的构建号?
- php - 如何为批量导入的 WordPress 帖子设置缩略图
- scala - 如何在 akka-http 请求中发送 json 对象数组
- html - 电子邮件客户端上的按钮看起来被压扁了
- python - 任何人都可以帮助正确使用 M2M 和模板吗?
- python - 生成的图像在 GAN 中未着色
- react-native - contentComponent 不工作反应本机导航