为什么laravel中不存在错误Builder :: query?

时间:2019-02-13 11:23:15

标签: php laravel laravel-5

在索引函数中,我根据角色和id查询,查询得到更改。

以下是索引方法中我控制器中的脚本

public function index(Request $request)
{

    $data = $request->validate([
        'id' => 'sometimes|nullable|string|max:10',
    ]);


    $networks = [];

    $user   = Auth::user();

    if($user->hasRole('admin')){
        $net = Network::query();
    }

    else if($user->hasRole('distributor')){
        $net = Network::whereHas('users', function($q) use ($user){
            $q->whereHas('roles', function($r){
                $r->whereIn('name', ['admin', 'owner']);
            })->where('parent_id', $user->id);
        })->query();
    }

    else if($user->hasRole('agent') || $user->hasRole('owner'))
        $net = Network::whereHas('users', function($q) use ($user){
            $q->where('email', $user->email);
        })->query();

    $ids_ordered = null;

    if(isset($data['id']) && $data['id']){
        $ids_ordered = $data['id'];
        $net = $net->orderByRaw(DB::raw("FIELD(id, $ids_ordered) desc"))
            ->paginate(3);

    }

    if($user->hasRole('admin') || $user->hasRole('distributor') || $user->hasRole('owner'))
        $ownerView = true;

    else $ownerView = false;

    $net = $net->get();
    if(!empty($net))

    $networks = $net->mapWithKeys(function($network) use ($ownerView, $ids_ordered){

        $coinSold   = $this->userNetworkRepo->getcoinSold($network->owner()->id);
        $allocated  = $this->userNetworkRepo->getAllocatedNez($network->owner()->id);
        $open       = false;

        if($ids_ordered){
            $ids_ordered = explode(',', $ids_ordered);

            if(in_array($network->id, $ids_ordered))
                $open = true;
        }

        $hierarchy = [
            'owner'             => $network->owner(),
            'agents'            => $network->agents(),
            'drawAgents'        => $this->drawAgentTree($network->id, true),
            'ownercoinSold'     => $coinSold,
            'ownerAllocated'    => $allocated,
            'open'              => $open,
        ];

        if ($distributor = $network->distributor()) {
           $hierarchy['distributor'] = $distributor;
        }

        $hierarchy['coinSold'] = [
            'ownerView'     => $ownerView
        ];

        return [$network->name => $hierarchy];

        //return [$network->distributor() ? $network->distributor()->getFullName() : 'no-partner' => $hierarchy];

    });

    if($request->ajax()){
        $view = view('admin.networks.partials.displayNetworkIndex', compact('networks'))->render();
        return response()->json(['html' => utf8_encode($view)],  200, ['Content-type'=> 'application/json; charset=utf-8'], JSON_UNESCAPED_UNICODE);
    }

    return view('admin.networks.index', compact('networks'));
}

但是当我访问页面时,出现以下错误

  

方法Illuminate \ Database \ Query \ Builder :: query不存在。

为什么显示此错误?该查询哪里出问题了?

我在Google上进行了搜索,发现了一些与keyBy相关的主题。

3 个答案:

答案 0 :(得分:1)

您无法在query()上致电Illuminate\Database\Query\Builder。之所以发生这种情况,是因为您要在哪里调用哪里,然后再次在其上调用query()

只需将其更新为:

if($user->hasRole('admin')){
        $net = Network::query();
    }

    else if($user->hasRole('distributor')){
        $net = Network::whereHas('users', function($q) use ($user){
            $q->whereHas('roles', function($r){
                $r->whereIn('name', ['admin', 'owner']);
            })->where('parent_id', $user->id);
        });
    }

    else if($user->hasRole('agent') || $user->hasRole('owner'))
        $net = Network::whereHas('users', function($q) use ($user){
            $q->where('email', $user->email);
        });

要进一步调试此类问题,您可以使用php artisan tinker并且可以弄乱

答案 1 :(得分:0)

启动查询时,您可以像从模型中一样从模型中静态调用100%,但是稍后在添加query()条件时,无需在无论如何它都会返回查询实例,因此基本上删除您的where调用,它将像这样工作:

query()

答案 2 :(得分:0)

方法“查询”仅在您已经调用了get结果方法之一(get,first,all等)或被静态调用时才起作用。尝试改用getQuery方法:

if($user->hasRole('admin')){
    $net = Network::query();
}

else if($user->hasRole('distributor')){
    $net = Network::whereHas('users', function($q) use ($user){
        $q->whereHas('roles', function($r){
            $r->whereIn('name', ['admin', 'owner']);
        })->where('parent_id', $user->id);
    })->getQuery();
}

else if($user->hasRole('agent') || $user->hasRole('owner'))
$net = Network::whereHas('users', function($q) use ($user){
    $q->where('email', $user->email);
})->getQuery();