Laravel 5.4:调用未定义的方法Illuminate / Database / Query / Builder :: getRelated()

I've been working on this for a week now, what I want to happen is that, to select the Level Rewards that have not been claimed by the user, but I'm getting an error which says: call to undefined method::getRelated().

The claim_rewards table consists of

id, reward_id, user_id, status

So, if a user have 3 rewards to be claimed, and the user claims one reward, only the two remaining rewards should be displayed on the field, so I supposed a 'whereDoesNotHave' clause should work out, but I can't figure out how to fulfill the code to make it work.

Here is the code of the two scopes:

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

public function scopeRewards($query)
{
   return $query = LevelRewards::whereDoesntHave('claimLevel')->whereDoesntHave('claim_rewards.status','=','0')->get();
}

I even tried this:

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

I even included this on the LevelRewards model:

use App\ClaimReward;

But I still get the same error. Any ideas on how to deal with this? Thank you.

Edit:

Here is the structure for the level_rewards table

id, level_required, item_name

That is why I made a scope into the LevelRewards model which is

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

So that it would result to a constraint relationship to claim_rewards table

So if I put it into a Scenario, this will be the outcome:

I, as a user is on a level 20. I have three rewards to claim. Now, for my lists of rewards, I have level 10, 15, and 20 rewards to claim. Now, I claimed the level 10 reward except for levels 15 and 20 rewards. So inside my claim_rewards table will have a data of that from level_rewards table that was claimed. Which will be the reward_id of the claim_rewards table.

So, if the level 10 reward that was claimed has a status of zero, then it will no longer be displayed into the field. The remaining rewards to be displayed is that those from level_rewards table that doesn't have a status of zero in the claim_rewards table

I suspect that you are using Illuminate\Database\Query\Builder in query scope, but you should instead use Illuminate\Database\Eloquent\Builder.

UPDATE

After a careful review of your code, I realized you are using whereDoesntHave with query scopes, but it can only be used with relationships. I suggest you refactor your query.

I have solved this issue by adding a method but I just have 1 small problem, I cannot add another whereDoesntHave clause for the claim_reward.status:

public function rewards()
{
    return $this->hasMany('App\ClaimReward','reward_id');
}

public function scopeNotClaimed($query)
{
    return $query->whereDoesntHave('rewards');
}