duanji2772 2019-04-17 04:52
浏览 90

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

  • 写回答

2条回答 默认 最新

  • dongzantai7570 2019-04-17 05:11
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP