雄辩请求:列ID不明确 [英] Eloquent request : column ID ambiguous
本文介绍了雄辩请求:列ID不明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ b我想通过Eloquent来获取表的所有行,其中ID不在数组
$ ids
中。 $ b $ product_deleteds = $ category-> products() - > whereNotIn('id',$ ids) - > get();
我收到这个错误:
SQLSTATE [23000]:完整性约束违规:1052 where子句中的列'id'不明确
请求这是一个:
从`products`
内部连接`products_categories`选择*
` products_categories`.`id` =`products`.`product_category_id`
其中`products`.`deleted_at`是null
和`products_categories`.`restaurant_id` = 1
和`id` = 4限制1
我知道我可以这样提出我的请求:
$ product_deleteds = $ category-> products() - > whereNotIn('products.id',$ id) - > get();
但是我不想要,因为表的名称可能会改变。我也可以这样做,但似乎有点棘手:
$ product_deleteds = $ category-> products() - > whereNotIn(Product :: getTableName()。'。id',$ ids) - > get();
任何帮助?
解决方案
首先,你不能这样做:
$ product_deleteds = $ category-> products () - > whereNotIn(
Product :: getTableName()//没有这样的方法,绝对不是静态的,除非你创建一个
。'。id',$ ids) - > get() ;
但是您可以这样做,以避免硬编码任何东西:
$ relatedKey = $ category-> products() - > getRelated() - > getQualifiedKeyName();
$ product_deleteds = $ category-> products() - > whereNotIn($ relatedKey,$ id) - > get();
I'm trying to get all rows of a table with Eloquent where the ID is not in an array $ids
.
$product_deleteds = $category->products()->whereNotIn('id', $ids)->get();
I got this error :
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
The request produce is this one :
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
I know I could make my request like this :
$product_deleteds = $category->products()->whereNotIn('products.id', $ids)->get();
But I don't want because the name of the table could change. I could to this too, but it seems to be a little tricky :
$product_deleteds = $category->products()->whereNotIn(Product::getTableName().'.id', $ids)->get();
Any help ?
解决方案
First off, you couldn't do that:
$product_deleteds = $category->products()->whereNotIn(
Product::getTableName() // no such method and definitely not static, unless you create one
.'.id', $ids)->get();
But you can do this in order to avoid hard-coding anything:
$relatedKey = $category->products()->getRelated()->getQualifiedKeyName();
$product_deleteds = $category->products()->whereNotIn($relatedKey, $ids)->get();
这篇关于雄辩请求:列ID不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文