使用带有 Laravel 的数据表时出现无效参数编号错误
Invalid parameter number error using DataTables with Laravel
当 DataTables 尝试获取数据时,它总是在这个 Eloquent 查询中被绊倒:
$items = Item::select([
DB::raw("images.url AS image"),
'items.id',
'items.sku',
'items.quantity',
DB::raw("IF(items.enabled, 'Yes', 'No') AS enabled")
])
->leftJoin('images', function ($j) {
$j->on('images.imageable_id', '=', 'items.id')
->where('images.imageable_type', '=', 'Item');
})
->leftJoin('order_items', 'items.id', '=', 'order_items.item_id')
->leftJoin('orders', 'orders.id', '=', 'order_items.order_id')
->where('items.store_id', 1)
->whereNull('items.deleted_at')
->whereIn('items.status', ['active', 'submitted'])
->groupBy('items.id');
查询工作正常,returns 得到了想要的结果。但是,DataTables 试图将其转换为以下内容,这会产生错误:
select count(*) as aggregate from (select '1' as row from `items` left join `images` on `images`.`imageable_id` = `items`.`id` and `images`.`imageable_type` = 1 left join `order_items` on `items`.`id` = `order_items`.`item_id` left join `orders` on `orders`.`id` = `order_items`.`order_id` where `items`.`store_id` = 1 and `items`.`deleted_at` is null group by `items`.`id`) AS count_row_table
这会具体产生此错误:
SQLSTATE[HY093]: Invalid parameter number
/home/vagrant/Projects/test.dev/vendor/laravel/framework/src/Illuminate/Database/Connection.php#301
当我直接在 MySQL 数据库上执行该查询时,没有问题。这似乎只发生在 Laravel 之内。
如果我删除查询的 ->leftJoin('images', function ($j) {...}
部分则没有错误,但我需要图像的连接。
如何解决这个错误?
在 AJAX:
上返回到 DataTables 的完整错误输出
{
"error":{
"type":"Illuminate\Database\QueryException",
"message":"SQLSTATE[HY093]: Invalid parameter number (SQL: select count(*) as aggregate from (select '1' as row from `items` left join `images` on `images`.`imageable_id` = `items`.`id` and `images`.`imageable_type` = 1 left join `order_items` on `items`.`id` = `order_items`.`item_id` left join `orders` on `orders`.`id` = `order_items`.`order_id` where `items`.`store_id` = active and `items`.`deleted_at` is null and `items`.`status` in (submitted, ?) group by `items`.`id`) AS count_row_table)",
"file":"\/home\/vagrant\/Projects\/test.dev\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connection.php",
"line":625
}
}
我确实遇到了这个问题。解决方法并不完美,因为它基本上会抓取所有数据两次,但这是我让它工作的唯一方法。
您必须在发送到 ->make();
之前执行 ->get();
。我真诚地希望有人找到正确的解决方案,但现在:
临时解决方案:
$data = DB::table('some_table');
$data->leftJoin('some_other_table', function($join)
{
$join->on('some_table.id', '=', 'some_other_table.id')
->where('some_table.something', '=', 'some_value');
});
$data->get();
return Datatables::of($data)->make();
这是使用 Laravel 的数据表包:https://github.com/bllim/laravel4-datatables-package
我今天遇到了类似的问题,不是 DataTables,而是使用独立查询构建器构建的复杂查询。该问题与左连接的问题类似,通过闭包来构建更复杂的连接条件。我在 "on" 之后使用了 where 条件。
解决方案是链接 "on" 调用:
->leftJoin('images', function ($j) {
$j->on('images.imageable_id', '=', 'items.id')
// note the "on" rather than the "where" and the use of a raw statement
->on('images.imageable_type', '=', DB::raw('Item'));
})
当 DataTables 尝试获取数据时,它总是在这个 Eloquent 查询中被绊倒:
$items = Item::select([
DB::raw("images.url AS image"),
'items.id',
'items.sku',
'items.quantity',
DB::raw("IF(items.enabled, 'Yes', 'No') AS enabled")
])
->leftJoin('images', function ($j) {
$j->on('images.imageable_id', '=', 'items.id')
->where('images.imageable_type', '=', 'Item');
})
->leftJoin('order_items', 'items.id', '=', 'order_items.item_id')
->leftJoin('orders', 'orders.id', '=', 'order_items.order_id')
->where('items.store_id', 1)
->whereNull('items.deleted_at')
->whereIn('items.status', ['active', 'submitted'])
->groupBy('items.id');
查询工作正常,returns 得到了想要的结果。但是,DataTables 试图将其转换为以下内容,这会产生错误:
select count(*) as aggregate from (select '1' as row from `items` left join `images` on `images`.`imageable_id` = `items`.`id` and `images`.`imageable_type` = 1 left join `order_items` on `items`.`id` = `order_items`.`item_id` left join `orders` on `orders`.`id` = `order_items`.`order_id` where `items`.`store_id` = 1 and `items`.`deleted_at` is null group by `items`.`id`) AS count_row_table
这会具体产生此错误:
SQLSTATE[HY093]: Invalid parameter number
/home/vagrant/Projects/test.dev/vendor/laravel/framework/src/Illuminate/Database/Connection.php#301
当我直接在 MySQL 数据库上执行该查询时,没有问题。这似乎只发生在 Laravel 之内。
如果我删除查询的 ->leftJoin('images', function ($j) {...}
部分则没有错误,但我需要图像的连接。
如何解决这个错误?
在 AJAX:
上返回到 DataTables 的完整错误输出{
"error":{
"type":"Illuminate\Database\QueryException",
"message":"SQLSTATE[HY093]: Invalid parameter number (SQL: select count(*) as aggregate from (select '1' as row from `items` left join `images` on `images`.`imageable_id` = `items`.`id` and `images`.`imageable_type` = 1 left join `order_items` on `items`.`id` = `order_items`.`item_id` left join `orders` on `orders`.`id` = `order_items`.`order_id` where `items`.`store_id` = active and `items`.`deleted_at` is null and `items`.`status` in (submitted, ?) group by `items`.`id`) AS count_row_table)",
"file":"\/home\/vagrant\/Projects\/test.dev\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connection.php",
"line":625
}
}
我确实遇到了这个问题。解决方法并不完美,因为它基本上会抓取所有数据两次,但这是我让它工作的唯一方法。
您必须在发送到 ->make();
之前执行 ->get();
。我真诚地希望有人找到正确的解决方案,但现在:
临时解决方案:
$data = DB::table('some_table');
$data->leftJoin('some_other_table', function($join)
{
$join->on('some_table.id', '=', 'some_other_table.id')
->where('some_table.something', '=', 'some_value');
});
$data->get();
return Datatables::of($data)->make();
这是使用 Laravel 的数据表包:https://github.com/bllim/laravel4-datatables-package
我今天遇到了类似的问题,不是 DataTables,而是使用独立查询构建器构建的复杂查询。该问题与左连接的问题类似,通过闭包来构建更复杂的连接条件。我在 "on" 之后使用了 where 条件。
解决方案是链接 "on" 调用:
->leftJoin('images', function ($j) {
$j->on('images.imageable_id', '=', 'items.id')
// note the "on" rather than the "where" and the use of a raw statement
->on('images.imageable_type', '=', DB::raw('Item'));
})