在laravel中除了自我之外的随机结果

时间:2018-04-18 03:34:53

标签: php laravel

我有这段代码:

$random

product two到哪里我得到了我的产品的相关结果。

问题是:

如果我在随机部分访问9 other products页面,我会看到product two + 10 different products

我想要的是

我正在访问product two

时,在我的随机部分中完全看到product two page而没有看到batches

我该怎么做?

2 个答案:

答案 0 :(得分:1)

你在SQ​​L中尝试过[WHERE NOT IN]吗?

$random = Product::whereNotIn('id', [$product->id])
->inRandomOrder()
->limit(10)
->get();

答案 1 :(得分:0)

您可以使用where过滤

$product = Product::where('slug', $slug)->firstOrFail();
$random = Product::where('id', '!=', $product->id)
    ->inRandomOrder()
    ->limit(10)
    ->get();