简体   繁体   English

如何在laravel集合的同一查询中使用laravel命令'whereIn'和'WhereNotIn'

[英]How to use the laravel command 'whereIn' and 'WhereNotIn' in the same query on laravel collection

I have a query that works fine when randomly selecting a new product from the database. 当从数据库中随机选择新产品时,我有一个查询工作正常。 But to prevent duplication, in my view I pass a JSON object of products already for sale on the page. 但是为了防止重复,我认为我在页面上传递了已经出售的产品的JSON对象。 I want my query to essentially query my DB for products for sale but are also not in the collection of products already for sale. 我希望我的查询本质上是在数据库中查询要出售的产品,但也不在已经出售的产品集合中。

This part works absolutely fine 这部分工作完全正常

$nproduct = Product::whereIn('seller_id', Auth::user()->samegroup())->inRandomOrder()->first();
            $nprice = $nproduct->price;
            $nquantity = $nproduct->quantity_available;
            $nid = $nproduct->id;
            $nseller = $nproduct->seller_id;

But I struggle when trying to make sure my query doesn't include anything inside $products_already_for_sale 但是当我尝试确保我的查询在$ products_already_for_sale中不包含任何内容时,我很努力

$products_already_for_sale = $request->current_product_ids;

try this 尝试这个

$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();

totally untested, but I think this would work 完全未经测试,但我认为这会起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM