简体   繁体   English

在 Laravel 5.4 中使用 whereDoesntHave()

[英]Working with whereDoesntHave() in Laravel 5.4

How do I implement a scope where in the user will claim a reward on a specific level and once they claim their rewards, the reward field will no longer display the reward that they have claimed.我如何实现用户将在特定级别领取奖励的范围,一旦他们领取奖励,奖励字段将不再显示他们领取的奖励。 So for example:例如:

The user is on level 20, on his reward panel, there are 3 available rewards to be claimed, level 10, 15 and 20. So if the user claimed the level 10 reward, it will no longer display on the panel except for level 15 and 20 rewards.用户在20级,在他的奖励面板上,有3个可领取的奖励,10级,15级和20级。所以如果用户领取了10级奖励,除了15级外,面板上不再显示和20个奖励。

Here is the code for the blade:这是刀片的代码:

 @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

And this is the code for the model ClaimReward model:这是模型 ClaimReward 模型的代码:

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

Any links and source of information regarding this question will be highly appreciated.非常感谢有关此问题的任何链接和信息来源。 Thank you.谢谢你。

Edit: I tried;编辑:我试过了;

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.*');
}

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

add another table that will hold rewards thats user already claimed,添加另一个表来保存用户已经领取的奖励,

and in the foreach check if this reward id exists in this user claimed rewards,并在 foreach 中检查此用户领取的奖励中是否存在此奖励 ID,

if not so show it and if yes so skip it.如果不是,则显示它,如果是,则跳过它。

the table will hold 2 fields:该表将包含 2 个字段:

1) user_id 1) 用户名

2) rewards_id 2)奖励_id

regarding the comments above,关于上述评论,

and after your edit to what you tried,在你编辑你尝试过的内容之后,

looks like the order of the query is wrong... put whereDoesntHave before the select,看起来查询的顺序是错误的...将 whereDoesntHave 放在选择之前,

and change to implementation of it as wrote in Laravel documentation example.并更改为 Laravel 文档示例中所写的它的实现。

try this:试试这个:

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.*');    

} }

edit:编辑:

try to change the join, as wrote here: https://laravel.com/docs/5.4/queries#joins尝试更改连接,如此处所写: https ://laravel.com/docs/5.4/queries#joins

build the join and in it's function set how to join and where status=0构建连接并在其功能中设置如何连接以及状态=0的位置

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM