gpt4 book ai didi

php - 类 Closure 的对象无法转换为字符串 laravel

转载 作者:行者123 更新时间:2023-11-29 16:11:27 25 4
gpt4 key购买 nike

我正在对该查询进行多次迭代,我假设我在查询中遗漏了某些内容,导致我收到如下错误:

Object of class Closure could not be converted to string

我的查询:

$test = UnitStandard::select(DB::raw("
count(t1.code),
t1.code,
t1.id,
t1.provider_id,
t1.name,
t1.nqf_level,
t1.credits,
t1.type,
MAX(t1.shared_id),
setas.name as setaName,
providers.name as providerName
"))
->from(function ($query){

$query->select(DB::raw("*"))
->from("unit_standards")
->where("provider_id", "=", 1)
->whereNull("shared_id")
->orWhere("shared_id", "=", 2)
->orderBy("unit_standards.code", "asc");
})
->join('setas', 't1.seta_id', '=', 'setas.id')
->join('providers', 't1.provider_id', '=', 'providers.id')
->where('shared_id', '=', 2)
->orWhere('t1.provider_id', '=', 1)
->groupBy('code')
->havingRaw('COUNT(code) >= 1')
->orderBy('code', 'asc')
->paginate(50);

更新

请参阅下面的完整错误代码段

enter image description here

更新

原始工作 MySQL:

 SELECT 
count(t1.code),
t1.code,
t1.id,
t1.provider_id,
t1.name,
t1.nqf_level,
t1.credits,
t1.type,
MAX(t1.shared_id),
setas.name as setaName,
providers.name as providerName
FROM(
SELECT * FROM unit_standards WHERE (provider_id = 1 AND shared_id IS NULL) OR shared_id = 2 ORDER BY `unit_standards`.`code` ASC
) AS t1
JOIN setas on t1.seta_id = setas.id
JOIN providers on t1.provider_id = providers.id
WHERE t1.shared_id = 2 OR t1.provider_id = 1
GROUP BY code
HAVING COUNT(code) >= 1
ORDER BY code ASC
LIMIT 50

最佳答案

感谢@NigelRen 的建议,我能够解决我遇到的问题。理想情况下,我希望通过单个查询获得相同的结果,但目前这样就可以了。

找到解决方案in this question :

 $subQuery = DB::table('unit_standards')->selectRaw('nqf_level,name, id, code, provider_id, shared_id, COUNT(code) as totalCodeCount, credits, type, seta_id')
->where('provider_id', '=', '1')
->whereNull('shared_id')
->orWhere('shared_id', '=', '2')
->groupBy('id','code','provider_id', 'shared_id', 'name', 'nqf_level', 'credits', 'type', 'seta_id')
->orderBy("code", "asc");


$sharedUnitstandards = \DB::table(\DB::raw('('.$subQuery->toSql().') as t1'))
->selectRaw("t1.code, t1.id, t1.provider_id, t1.name, t1.nqf_level, t1.credits, t1.type, MAX(t1.shared_id) ,setas.name as setaName, providers.name as providerName")
->mergeBindings($subQuery)
->join('setas', 't1.seta_id', '=', 'setas.id')
->join('providers', 't1.provider_id', '=', 'providers.id')
->where('t1.shared_id', '=', 2)
->orWhere('t1.provider_id', '=', 1)
->groupBy('t1.code','t1.code','t1.id','t1.provider_id','t1.name', 't1.nqf_level', 't1.credits','t1.type','t1.shared_id', 't1.seta_id', 'setas.name', 'providers.name')
->havingRaw('COUNT(t1.code) >= 1')
->orderBy('t1.code', 'asc')
->paginate(50);

关于php - 类 Closure 的对象无法转换为字符串 laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55215769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com