Laravel联合查询500错误

问题描述 投票:0回答:1

所以我有以下代码...

$query = DB::table('recipes');

// USING ONE OF THE FF STATEMENTS CHANGES THE OUTPUT
$second = DB::table('recipes'); // WORKS
$second = $query; // BREAKS AND RETURNS 500 ERROR

$query->where('status', 0);
$query->where('type', 'x');

$second->where('status', 0);
$second->where('type', 'y');

return $second->union($query)->toSql(); // toSql ONLY FOR DEBUG

[我只是想尽全力解释为什么尽管分配值本质上是相同的查询生成器对象,但这仍会出错。

php laravel
1个回答
0
投票

您的语法已关闭:

$first = DB::table('recipes')
        ->where('status', 0);
        ->where('type', 'x');

$second = DB::table('recipes')
        ->where('status', 0);
        ->where('type', 'y');
        ->union($first)
        ->get();
© www.soinside.com 2019 - 2024. All rights reserved.