Eloquent::where() 条件未按预期工作,但 Collection::where() 确实有效
Eloquent::where() condition is not working as expected, but Collection::where() does work
我已经使用 Eloquent 玩了一段时间,但我遇到了 Eloquent::where() 无法正常工作的情况。不过,我设法让 Collection::where() 工作了。但是,我仍然想知道为什么 Eloquent::where() 在我的情况下不起作用。我会尽量让问题简单化,这里是:
- 我有 Product 和 ProductCategory 作为多对多关系,主元 table 名称是“product_relate_category”。
- 这是他们之间的关系。它仍在运行,因此您可以跳过
Models/Product {
public function productCategory() {
return $this->belongsToMany(ProductCategory::class, 'product_relate_category','product_id','category_id');
}
}
Models/ProductCategory {
public function product() {
return $this->belongsToMany(Product::class, 'product_relate_category', 'category_id', 'product_id');
}
}
这是棘手的部分:我们需要显示相关产品,这些产品属于同一产品类别但具有不同的 ID。以第一个产品为例,任务是获取所有相关产品
foreach (Product::first()->productCategory()->get() as $category)
{
foreach($category->product()->where("id", "<>", Product::first()->id)->get() as $result)
{
echo($result->name);
}
}
好像在工作?不,它说:“违反完整性约束:1052 Column 'id' in where clause is ambiguous”。
现在,如果我省略上面的 Eloquent::where()
条件,我就可以访问所有带有“id”的产品,这是一个很大的混乱。
但是,如果我把它改成下面的任何一个,它就可以完美地工作
$category->product()->where("product_id", "<>", Product::first()->id)->get()
//注意“id”已更改为“product_id”
或
$category->product()->get()->where("id", "<>", Product::first()->id)
//Method order changed
- 在第二个实例中,Collection::where() 被调用了,我很不愿意使用它。那么对于这种情况有什么解释以及为什么 Eloquent 会这样吗?
在查询的 where 中,您使用了存在于 product_relate_category table 和产品 table 中的列 'id',因此数据库不能'不确定你的意思是什么列......
在这种情况下,您应该写下完整的列名:
$category->product()->where("products.id", "<>", Product::first()->id)->get();
我已经使用 Eloquent 玩了一段时间,但我遇到了 Eloquent::where() 无法正常工作的情况。不过,我设法让 Collection::where() 工作了。但是,我仍然想知道为什么 Eloquent::where() 在我的情况下不起作用。我会尽量让问题简单化,这里是:
- 我有 Product 和 ProductCategory 作为多对多关系,主元 table 名称是“product_relate_category”。
- 这是他们之间的关系。它仍在运行,因此您可以跳过
Models/Product {
public function productCategory() {
return $this->belongsToMany(ProductCategory::class, 'product_relate_category','product_id','category_id');
}
}
Models/ProductCategory {
public function product() {
return $this->belongsToMany(Product::class, 'product_relate_category', 'category_id', 'product_id');
}
}
这是棘手的部分:我们需要显示相关产品,这些产品属于同一产品类别但具有不同的 ID。以第一个产品为例,任务是获取所有相关产品
foreach (Product::first()->productCategory()->get() as $category) { foreach($category->product()->where("id", "<>", Product::first()->id)->get() as $result) { echo($result->name); } }
好像在工作?不,它说:“违反完整性约束:1052 Column 'id' in where clause is ambiguous”。
现在,如果我省略上面的
Eloquent::where()
条件,我就可以访问所有带有“id”的产品,这是一个很大的混乱。但是,如果我把它改成下面的任何一个,它就可以完美地工作
$category->product()->where("product_id", "<>", Product::first()->id)->get()
//注意“id”已更改为“product_id”
或
$category->product()->get()->where("id", "<>", Product::first()->id)
//Method order changed
- 在第二个实例中,Collection::where() 被调用了,我很不愿意使用它。那么对于这种情况有什么解释以及为什么 Eloquent 会这样吗?
在查询的 where 中,您使用了存在于 product_relate_category table 和产品 table 中的列 'id',因此数据库不能'不确定你的意思是什么列...... 在这种情况下,您应该写下完整的列名:
$category->product()->where("products.id", "<>", Product::first()->id)->get();