我有以下查询:
SELECT *
FROM instruments
LEFT join financials on instruments.id=financials.instruments_id
WHERE financials.id IN
( SELECT MAX(financials.id)
FROM financials
GROUP BY financials.instruments_id )
ORDER BY instruments.id ASC
以下是我雄辩的翻译:
$overviewArray = DB::table('instruments')
->leftJoin('financials', 'instruments.id', '=', 'financials.instruments_id')
->whereIn('financials.id', DB::raw('SELECT MAX(financials.id)
FROM financials
GROUP BY financials.instruments_id )
ORDER BY instruments.id ASC'))->toArray();
我想将结果作为数组返回,所以我使用toArray()
:
但是,我收到以下错误:
In Builder.php line 2461:
Call to undefined method Illuminate\Database\Query\Builder::toArray()
为什么会出现这种情况?
感谢您的回复!
更新
在我的查询结尾添加->get()
后,我收到以下错误:
在Grammar.php第135行:
Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, object given, called
in C:\Users\admin\Desktop\Coding Projects\demo\vendor\laravel\framework\src\Illuminate\Database\Query\Gramm
ars\Grammar.php on line 250
答案 0 :(得分:3)
您需要在查询中添加get()
才能执行它:
DB::table('instruments')->(....)->get()->toArray();
答案 1 :(得分:2)
获取查询构建器结果后,
pstm.setInt(1, Integer.parseInt(text.getText()));
使用$result = DB::table('instruments as i')
->leftJoin('financials as f', 'i.id', '=', 'f.instruments_id')
->whereIn('f.id', DB::raw('SELECT MAX(f.id) FROM financials as fs GROUP BY fs.instruments_id'))
->orderBy('i.id')
->get();
(array) $result
或$overviewArray = (array) $result;
转换为数组
json_decode(json_encode(...))
答案 2 :(得分:1)
toArray()
显示异常(单个记录)。
对于此,只需在使用toArray()
示例:
$data = DB::table('instruments')->(....)->first();
if($data!=null){
$arrayData = $data->toArray();
}