传递给Illuminate \ Database \ Grammar :: parameterize()的参数1必须是给定的对象类型数组

时间:2017-12-23 11:50:30

标签: laravel laravel-5 eloquent laravel-eloquent

我正在使用Laravel 5.5,我翻译了以下查询:

        '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'))->get()->toArray();

但是,我收到以下错误:

在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\laravel_project\vendor\laravel\framework\src\Illuminate\Database\Query\Gramm
  ars\Grammar.php on line 250

我的猜测是我的雄辩查询错了?有什么建议吗?

感谢您的回复!

2 个答案:

答案 0 :(得分:2)

 $overviewArray = DB::table('instruments')
    ->leftJoin('financials', 'instruments.id', '=', inancials.instruments_id')
    ->whereIn('financials.id', function($query){
      $query->select(DB::raw('MAX(financials.id)'))->
     from('financials')->
     groupBy('financials.instruments_id');})
    ->orderBy('instruments.id')
    ->get()
    ->toArray();

我想会没事的。

答案 1 :(得分:0)

就我而言,这是因为我在执行 Event::get(44) 而不是 Event::find(44)