Database 如何使用laravel命令';其中';和';其中不包括';在laravel集合的同一查询中

Database 如何使用laravel命令';其中';和';其中不包括';在laravel集合的同一查询中,database,laravel,eloquent,Database,Laravel,Eloquent,我有一个查询,在从数据库中随机选择新产品时可以很好地工作。但为了防止重复,在我看来,我在页面上传递了一个已经在销售的产品的JSON对象。我希望我的查询本质上是查询我的数据库中的待售产品,但也不在已待售产品的集合中 这部分工作绝对不错 $nproduct = Product::whereIn('seller_id', Auth::user()->samegroup())->inRandomOrder()->first(); $nprice = $nprod

我有一个查询,在从数据库中随机选择新产品时可以很好地工作。但为了防止重复,在我看来,我在页面上传递了一个已经在销售的产品的JSON对象。我希望我的查询本质上是查询我的数据库中的待售产品,但也不在已待售产品的集合中

这部分工作绝对不错

$nproduct = Product::whereIn('seller_id', Auth::user()->samegroup())->inRandomOrder()->first();
            $nprice = $nproduct->price;
            $nquantity = $nproduct->quantity_available;
            $nid = $nproduct->id;
            $nseller = $nproduct->seller_id;
但是,当我试图确保我的查询中没有包含$products\u products\u products\u products\u products\u products\u中的任何内容时,我感到很挣扎

$products_already_for_sale = $request->current_product_ids;
试试这个

$products_already_for_sale = $request->current_product_ids; //it must be array []

$nproduct = Product::whereIn('seller_id', Auth::user()->samegroup())->whereNotIn('product_id',
$products_already_for_sale)->inRandomOrder()->first();
完全未经测试,但我认为这会起作用

$products_already_for_sale = $request->current_product_ids; //it must be array []

$nproduct = Product::whereIn('seller_id', Auth::user()->samegroup())->whereNotIn('product_id',
$products_already_for_sale)->inRandomOrder()->first();

完全未经测试,但我认为这会起作用

为什么要使用
first()
执行
其中的
<代码>其中
用于当您有多个时。为什么要使用
first()
执行
其中
<代码>其中
用于当您有多个。