【问题标题】:How to use the laravel command 'whereIn' and 'WhereNotIn' in the same query on laravel collection如何在 laravel 集合的同一查询中使用 laravel 命令 'whereIn' 和 'WhereNotIn'
【发布时间】:2019-08-20 23:09:24
【问题描述】:

从数据库中随机选择新产品时,我的查询运行良好。但是为了防止重复,在我看来,我在页面上传递了一个已经在销售的产品的 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_already_for_sale 中的任何内容时遇到了困难

$products_already_for_sale = $request->current_product_ids;

【问题讨论】:

  • 你为什么要用whereInfirst()whereIn 用于当您有多个时。

标签: database laravel eloquent


【解决方案1】:

试试这个

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    相关资源
    最近更新 更多