Laravel与关系 - 未找到列

时间:2017-09-28 22:28:09

标签: laravel laravel-5.3 laravel-5.4 laravel-5.5

我使用此查询:

formControlName

$clients = Client::with("types", "results")->where(function ($query) use ($issued, $mode, $request) { if (($request->get("filter"))) { $query->where('issued', $issued); } } 关系是:

results

public function results() { return $this->hasOne('App\ResultTest', 'client_id', 'unique_code'); } 包含ResultTest'字段和client_id。它与当前表连接为:

issued

现在表Client.uqique_code = ResultTest.clients. 为空。

它给我一个错误:

ResultTest

我试过这种方式,建议在下面的答案中使用:

Unknown column 'issued' in 'where clause' (SQL: select count(*) as aggregate from `clients` where (`issued` = 1))

它给出了同样的错误:

$clients->with(["results" => function($query) use ($issued, $request, $mode) {
            $query->where('issues', $issued);

            if (($request->get("mode"))) {
                $query->whereIn('mode', $mode);
            }

        }])->orderBy('created_at', 'desc')->paginate();

1 个答案:

答案 0 :(得分:2)

经过长时间的聊天,我们找到了以下解决方案:

if($request->has('filter') || $request->has('mode')) { 
    $builder->whereHas('results', function ($query) use ($request) { 

        if (($request->has("filter"))) { 
            $query->where('issued', $request->get('filter')); 
        } 

        if (($request->has("mode"))) { 
            $query->whereIn('mode', $request->get('mode')); 
        } 
    }); 
}