douhuang1852 2016-07-24 08:37
浏览 39
已采纳

Laravel:获取有条件的记录

I have four models User,Reward,RewardUnlocked,RewardClaimed And the relationship between them is :

User have many RewardUnlocked,=>(Many to one)

User have many RewardClaimed=>(Many to one)

Reward have many RewardUnlocked=>(Many to one)

Reward have many RewardClaimed=>(Many to one)

The introduction of the problem is that a user will first unlock a reward when a specific condition becomes true,

Now the issue is that i want to fetch all the rewards from Reward table and show to the user, And check each reward that if the reward_id is present in RewardUnlocked then show a check sign if not present then show cross sign

The view file is a header file so i am sending the variable via ajax.

For the time being my code is

controller :

$rewards = App\Reward::all();
  foreach ($rewards as $reward){
    $list = App\User::with('unlocks')->where('user_id','=', Auth::user()->id)->first();
  }

The problem with the following code is that i have to send the reward list , And show there with a check sign or cross sign that you have unlocked the reward.

  • 写回答

1条回答 默认 最新

  • dongtaotao19830418 2016-07-24 10:40
    关注

    to be more clear, I understand that you need to show a list of reward in the user profile, with whatever rewards unlocked, a check sign respectively and rest a cross sign,
    if its the case, then you can use belongs to many relation with your user table to rewards table, and a status column to track reward claimed and reward unlocked and you can access it as pivot attribute, so whatever reward not in the middle table are not unlocked,
    you may read on the Laravel doc about Many to Many relation to understand further https://laravel.com/docs/5.2/eloquent-relationships#many-to-many

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题