Search code examples
phplaravellaravel-10

Laravel using inRandomOrder how to prevent duplicate record


I'm Using Laravel 10 I'm trying to get Random Record Without Duplication using inRandomOrder But It Giving Duplicate Record Some Time. help me to Solve It

My Controller Code

$Question = Question::inRandomOrder()->limit(50)->get();

Solution

  • To get random records without duplication using Laravel's inRandomOrder() method, you can use distinct() method to get unique records

    $questions = Question::inRandomOrder()
    ->limit(50)
    ->distinct()
    ->get();