随机获取 3 个数据并将其显示在 blade 上并获取另外 3 个数据而不复制已经显示的数据
Get 3 number of data randomly and show it on blade and get another 3 data without duplicating data that already show
我有一个问题,我在数据库中有 9 个数据,我想随机获取 3 个数据插入到 3 个组(A、B、C)中,我不希望任何数据重复,因为它是从数据库中随机抽取。
这是我控制器中的代码,我已经为 A 尝试过,但我不知道如何在没有任何数据重复的情况下为 B 和 C 获取接下来的 3 个随机数据。
$A = TemporarySubject::inRandomOrder()->where('subject_type','compulsory')->take(3)->get();
$B = // i dont know how to do this
$C = // i dont know how to do this
$A = TemporarySubject::inRandomOrder()
->where('subject_type','compulsory')
->take(3)
->get();
$B = TemporarySubject::inRandomOrder()
->where('subject_type','compulsory')
->whereNotIn('id', $A->pluck('id')) // <---
->take(3)
->get();
$A = TemporarySubject::inRandomOrder()
->where('subject_type','compulsory')
->whereNotIn('id', $A->pluck('id')) // <---
->whereNotIn('id', $B->pluck('id')) // <---
->take(3)
->get();
我有一个问题,我在数据库中有 9 个数据,我想随机获取 3 个数据插入到 3 个组(A、B、C)中,我不希望任何数据重复,因为它是从数据库中随机抽取。
这是我控制器中的代码,我已经为 A 尝试过,但我不知道如何在没有任何数据重复的情况下为 B 和 C 获取接下来的 3 个随机数据。
$A = TemporarySubject::inRandomOrder()->where('subject_type','compulsory')->take(3)->get();
$B = // i dont know how to do this
$C = // i dont know how to do this
$A = TemporarySubject::inRandomOrder()
->where('subject_type','compulsory')
->take(3)
->get();
$B = TemporarySubject::inRandomOrder()
->where('subject_type','compulsory')
->whereNotIn('id', $A->pluck('id')) // <---
->take(3)
->get();
$A = TemporarySubject::inRandomOrder()
->where('subject_type','compulsory')
->whereNotIn('id', $A->pluck('id')) // <---
->whereNotIn('id', $B->pluck('id')) // <---
->take(3)
->get();