Eloquent::where() 条件未按预期工作,但 Collection::where() 确实有效

Eloquent::where() condition is not working as expected, but Collection::where() does work

我已经使用 Eloquent 玩了一段时间,但我遇到了 Eloquent::where() 无法正常工作的情况。不过,我设法让 Collection::where() 工作了。但是,我仍然想知道为什么 Eloquent::where() 在我的情况下不起作用。我会尽量让问题简单化,这里是:


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');
    }
}

    $category->product()->get()->where("id", "<>", Product::first()->id) 
//Method order changed

在查询的 where 中,您使用了存在于 product_relate_category table 和产品 table 中的列 'id',因此数据库不能'不确定你的意思是什么列...... 在这种情况下,您应该写下完整的列名:

 $category->product()->where("products.id", "<>", Product::first()->id)->get();