gpt4 book ai didi

php - 将查询结果作为数组返回

转载 作者:行者123 更新时间:2023-12-03 23:27:12 28 4
gpt4 key购买 nike

我有以下查询:

            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

下面是我 Eloquent 翻译:

$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

最佳答案

您需要在查询中添加get() 来执行它:

DB::table('instruments')->(....)->get()->toArray();

关于php - 将查询结果作为数组返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47951819/

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