【发布时间】:2014-09-19 14:18:56
【问题描述】:
我正在尝试使用 Eloquent 获取表的所有行,其中 ID 不在数组 $ids 中。
$product_deleteds = $category->products()->whereNotIn('id', $ids)->get();
我收到了这个错误:
SQLSTATE[23000]:违反完整性约束:1052 where 子句中的列 'id' 不明确
请求产生的是这个:
Select *
from `products`
inner join `products_categories` on `products_categories`.`id` = `products`.`product_category_id`
where `products`.`deleted_at` is null
and `products_categories`.`restaurant_id` = 1
and `id` = 4 limit 1
我知道我可以这样提出我的要求:
$product_deleteds = $category->products()->whereNotIn('products.id', $ids)->get();
但我不想要,因为表的名称可能会改变。我也可以这样做,但这似乎有点棘手:
$product_deleteds = $category->products()->whereNotIn(Product::getTableName().'.id', $ids)->get();
有什么帮助吗?
【问题讨论】:
-
如果表名可能会改变,那么我会采用
Product::getTableName()方法