0

我如何实现一个范围,在该范围内,用户将在特定级别上领取奖励,并且一旦他们领取奖励,奖励字段将不再显示他们已领取的奖励。例如:

用户在20级,在他的奖励面板上,有3个可用的奖励可以领取,10级、15级和20级。所以如果用户领取了10级奖励,它将不再显示在面板上,除了15级和20个奖励。

这是刀片的代码:

 @foreach(App\LevelRewards::getRewards()->get() as $rewards)
 @if(Auth::user()->level >= $rewards->level_required || Auth::user()->level == $rewards->level_required)
    @if(App\ClaimReward::claimLevel()->get())
            <div class="col-lg-2 col-md-3 col-sm-6 col-xs-6" style="margin: 5px 0px;">
               <div class="hovereffect">
                    <a href="#" class="d-block mb-4 h-100">
                   <input type ="hidden" name="id" value="{{$rewards->id}}"/>
                   <img class="img-responsive" src="{{asset('rewards_img/'.$rewards->picture)}}" alt="">
                    </a>
                <div class="overlay">
                 <h2 style="font-size: 14px;">{{$rewards->details}}</h2>
                 @if($rewards->type == 'physical')
                 <button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#claimPhysical" data-details="{{$rewards->details}}" data-id="{{$rewards->id}}" data-level="{{$rewards->level_required}}" data-physical="{{$rewards->item_name}}">
                CLAIM
                </button>
                @else
                <button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#claimDigital" data-id="{{$rewards->id}}" data-level="{{$rewards->level_required}}" data-physical="{{$rewards->item_name}}">
                CLAIM
                </button>
                @endif
      </div>
    <div style="background: #2d2d2d; padding: 9px;">
         <span class="credits_top">Required Level: <span>{{$rewards->level_required}}</span></span>
         <p>{{$rewards->item_name}}</p> 
    </div> 
</div>                                                                  
   @endif
  @endif
@endforeach

这是模型 ClaimReward 模型的代码:

public function scopeClaimLevel2($query)
{
    return $query->join('level_rewards','level_rewards.id','=','claim_rewards.reward_id')->select('level_rewards.*');
}

任何有关此问题的链接和信息来源都将受到高度赞赏。谢谢你。

编辑:我试过了;

public function scopeClaimLevel($query)
{
    return $query->join('claim_rewards','claim_rewards.reward_id','=','level_rewards.id')->select('level_rewards.*');
}
public function scopeGetRewards($query)
{
    return $query
    ->join('claim_rewards','claim_rewards.reward_id','=','level_rewards.id')
    ->whereDoesntHave('claimLevel', function (Builder $query) {
           $query->where('claim_rewards.status', '=', '0');
     }) 
    ->select('level_rewards.*');
}

但它说 Call to undefined method Illuminate\Database\Query\Builder::getRelated()'

4

2 回答 2

0

关于上面的评论,

在您编辑您尝试过的内容后,

看起来查询的顺序是错误的......把 whereDoesntHave 放在 select 之前,

并更改为 Laravel 文档示例中所写的实现。

尝试这个:

public function scopeClaimLevel2($query)
{
    return $query
      ->join('claim_rewards','claim_rewards.reward_id','=','level_rewards.id')
      ->whereDoesntHave('claim_rewards', function (Builder $query) {
             $query->where('status', '=', '0');
       }) 
      ->select('level_rewards.*');    

}

编辑:

尝试更改联接,如此处所述:https ://laravel.com/docs/5.4/queries#joins

建立连接并在它的函数中设置如何连接以及 status=0

return $query
      ->join('claim_rewards', function ($join) {
          $join->on('claim_rewards.reward_id', '=', 'level_rewards.id')->where('status', '=', '0');
      })
      ->select('level_rewards.*');
于 2019-04-16T06:07:46.740 回答
0

添加另一个表格,该表格将保存用户已经领取的奖励,

并在 foreach 中检查此用户要求的奖励中是否存在此奖励 ID,

如果不是这样显示它,如果是,那么跳过它。

该表将包含 2 个字段:

1) 用户 ID

2)rewards_id

于 2019-04-15T08:37:41.553 回答